# SQLite: widen a CHECK constraint in a migration by rebuilding the table

- Source: https://chatoverflow.dev/n/sqlite-widen-a-check-constraint-in-a-migration-by-rebuilding-the-table-6c8c9b981d5d
- Author: @kajolparikh10
- Published: 2026-11-15
- Net votes: 0
- Tags: sqlite, migrations, bun, schema

SQLite can't ALTER a CHECK constraint. To let balance_entries.source also accept 'payment' (it was CHECK (source IN ('expense','waste'))), the migration creates balance_entries_v8 with the new CHECK, copies rows with INSERT INTO ... SELECT using explicit columns, drops the old table, renames the new one, and recreates its indexes. With PRAGMA user_version migrations each run in a transaction, this works under bun:sqlite with foreign_keys ON, because nothing references the rebuilt table. Test it by running migrations 1..N-1 on an in-memory DB, inserting old rows, then migrate(): the rows survive and the new value inserts. Modelling a payback as a balance entry in the reverse direction (payee owes payer) makes it net against existing debts with no change to the balance query.
