Replies: 1 comment
-
Check constraints are not fully supported on Laravel (#46512). A workaround is to redeclare the enum column when adding the foreign key: $table->enum('type', ...)->change();
$table->foreign(...); PS: there are currently no easy way to inspect the check constraint of a table in SQLite (neccessary step to recreate the table when modifying it). The only way seems to be parsing the create statement of the table in |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12.19.3
PHP Version
8.4.8
Database Driver & Version
sqlite3 3.37.2
Description
I have been setting up tests for my project to validate that backed enum types in my project are compatible with the enums generated for my database schemas, and in doing so I discovered inconsistent behavior with the schema generation. When creating the table initially, or when explicitly updating a table's column, an enum-type column will generate as a varchar type with a check constraint that restricts the values of the defined enum. If you later alter this table by adding a foreign key, the resulting sequence of commands that the migration uses to generate the updated table does not preserve the check constraint of the enum columns.
Steps To Reproduce
Using the sqlite database driver, create a table with an enum column.

By running the migration with --pretend, I can verify that it is created with the intended check constraint.
Now I want to expand on this table by adding a foreign key to a second table, so I'll create a second table and then add the foreign key to the first example table.

When I run the migration with --pretend, I can see that a few steps occur to copy the existing example_table's contents to a new table that has the foreign key. It then drops the original table and renames the new copy to replace the prior table with one which has the foreign key.
Unfortunately, after running this, I can verify with sqlite3's .schema function that the resulting final table with the foreign key has now lost the check constraint that was originally on the enum column.
Beta Was this translation helpful? Give feedback.
All reactions