Sometimes situations arise in Laravel when creating separate database tables, when the order migrations affects the successful execution of their deployment operations.
For example, the situation
You are doing a project on Laravel, and it became necessary to add a new table to the database (unit_orders
- as an example), which will be linked by its primary key to the table (employers
- as an example) previously created.
Before adding a new table (by migration), we roll back all existing migrations to their original state with the command php artisan migrate:rollback
.
Next, we create a new migration for our new table using the php artisan make:model UnitOrder -mcr
command.
As a result, the "bouquet" we need is created - the controller, the model and the migration. But the created migration ends up at the bottom.
In the migrations
folder, the migration files will be arranged in chronological order from the dates (timestamps) of their creation.
If we roll out all the migrations for our project now, we will get the following type of error:
"An already created table cannot add the foreign key of a table that does not yet exist."
Thus, in order to perform all migrations correctly (creating all project tables), it is necessary to transfer the last migration to create our new unit_orders
table before the previously created employers
table. This is done by changing the date of its creation to an arbitrary date, which must precede the date of creation of the existing employers
table.
Before performing migrations, completely clean the database (this can even be done manually via the graphical database client if any errors occur on the part of the php artisan migrate:rollback
team).
After that, we run the standard command php artisan migrate
, and all migrations are successful.