Reputation: 2639
I had a table neighboorhoods and a table schools with a neighboorhood_id field.
When I saw that it was misspelled I wanted to change the name to the correct form.
First I used phpStorm's name refactor tool and changed the model to Neighborhood, then I added the $table = 'neighboorhood'.
Then I made a migration to rename the table, deleted the $table property and everything is working fine.
I thought it was going to throw an error because the foreign key constraint was created this way:
$table->foreign('neighboorhood_id')->references('id')->on('neighboorhoods')->onDelete('cascade');
And now it works without having to update it to:
$table->foreign('neighborhood_id')->references('id')->on('neighborhoods')->onDelete('cascade');
Why didn't I have to modify the constraint? I checked the constraint in the database and now it's pointing to neighborhoods table instead of neighboorhoods.
Upvotes: 1
Views: 1995
Reputation: 1320
I've also had such a situation. Apparently InnoDB doesnt use the table name as a reference to the constraint: it uses some internal identity instead (not sure about other database types).
Think about it as an ID
, which stays the same when the name
of the table changes.
However, the docs say something different, so dropping and creating a new foreign key would definitely be a good thing to do.
Foreign keys that point to the renamed table are not automatically updated. In such cases, you must drop and re-create the foreign keys in order for them to function properly.
Upvotes: 1