-
Couldn't load subscription status.
- Fork 1.5k
Description
I'm trying to do a migration involving adding a constraint to a field within a table that is referenced as a foreign key by other tables in SQLite. As you might know, SQLite's ALTER syntax is quite limited, so the best way to do this is by creating a new temporary table, inserting all the data from the original table, dropping the old table and renaming the temporary table to take its place.
This is where the problem comes in! Removing the original table invalidates all those foreign key constraints. The normal fix for this is to run PRAGMA foreign_keys = OFF; at the start of the migration, and then PRAGMA foreign_keys = ON; at the end. However, this can't be set while in the middle of a transaction... and sqlx, very sensibly, performs all migrations within a transaction.
I'm not sure exactly what the best resolution to this is - it seems like it would be pretty fussy to add options to disable foreign key enforcement temporarily, or not wrap migrations within a transaction. Possibly it would be reasonable to automatically run PRAGMA defer_foreign_keys = ON at the start of every migration?
Additionally, I will say that I was confused for a little while as to why the migration would work when I applied it manually, but wouldn't work when it was run from the command line (the answer is that sqlx defaults PRAGMA foreign_keys to be off).