[8.x] Throw an exception when trying to add a foreign key to an existing SQLite database #32583
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I just spent an hour trying to figure out why a foreign key constraint wasn't working in one of my tests. As it turns out, SQLite doesn't support adding foreign keys to existing databases. Doing this in a migration doesn't throw an exception, you'll only notice your foreign key constraints aren't working when you start seeing weird behavior in your code (such as
ON DELETE CASCADE
not working).What makes this issue even harder to debug, is that when you look up "SQLite foreign key constraints not working", all the answers you find will say "foreign keys are disabled by default, you should enable them manually". Laravel has been enabling foreign keys for SQLite since #26298, so all these answers aren't useful. You have to specifically look up "add foreign key to SQLite table" to figure out why it isn't working.
This PR makes it so that an exception is thrown if you try to add a foreign key to an existing SQLite database. It also adds tests for the exceptions that are thrown when you try to run commands that aren't available for SQLite.
I'm targeting 8.x because this change will likely break some users their migrations.