ramazankemal's avatar

a question about migration

Hello there, Now I have a question like this, I hope I can express myself. I am using Laravel 8. Let me explain through this example; There is User and Post migration. The post model should contain the "user_id" field as it is. Now the problem is 1- php artisan make: model Post -m 2- php artisan make: model User -m

If I create the commands in the above order, the command "php artisan migrate" will show me an error, the error is likely to look like this:

"relation" users "does not exist (SQL: alter table" posts "add constraint" posts_user_id_foreign "foreign key (" user_id ") references" users "(" id ") on delete cascade)"

In fact, if I created the User model before the Post model, there would be no problem. When I want to create a new table while I am progressing in the project, I encounter such problems, in this case I need to delete the migration file with foreign keys and create it again. How can I get rid of it. How can I make it work without deleting the files again?

0 likes
2 replies
badreddine jeddab's avatar

did you try to change the name of the files in migrations folder, cause it appear that order of those files depend on the date , try to edit the date on files maybe that will fix your problem

2 likes
siangboon's avatar

you have to ensure the table exist before you can reference it, the sequence does matter...

you can also create tables without any relationship first, and alter the table once you need to setup the relationship...

1 like

Please or to participate in this conversation.