Home > Net >  Foreignkey in migrate laravel
Foreignkey in migrate laravel

Time:09-11

I have a problem when migrate in laravel. Ex: I have 2 migrate: products and categories. The products has a foreignkey to categoried .If I create products migration before categories migration, when I run php artisan migrate, It cann't migrate. But if I create the categories first, it works. Can I run it without create again or rename file?

Thanks.

CodePudding user response:

the migration must created with order, the file without foreign key before the file with foreign key because they must now the reference

CodePudding user response:

Migrations are run in the order you have defined them (which is by the date in the filename database/migrations/2022_01_01_000100_create_my_table.php). If for some reason this order does not work for you because for instance some foreign key requires another table to exist, then you have to change the order of these migrations by changing the filename for instance.

However, it depends on whether you have ran the migrations before on production. If you did, there should be a migrations table which contains all the records of the migrations that have been ran. If migrating doesn't work for you on the production server it is often easier to fix it manually and make sure the migrations table is up-to-date.

Just remember that you shouldn't be editting migration files themselves for this exact reason, because already-ran migrations will not take your changes into account.

  • Related