8

I want to disable foreign key constraint on a relationship because I have a problem after:

php artisan migrate:refresh

How can I solve it?

parastoo
  • 2,211
  • 2
  • 21
  • 38

2 Answers2

8

In 5.5 you can just use the fresh command:

php artisan migrate:fresh

The migrate:fresh command will drop all tables from the database and then execute the migrate command

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
8

You can disable foreign key constraints in your migration file.

Schema::disableForeignKeyConstraints();

Ref: Laravel migrations nice way of disabling foreign key checks

  • 1
    This answer was chosen as the best answer and also upvoted twice. So, could someone explain me why do you want to disable FK constraints (which is already a terrible way to work with migrations) and then enable those in each migration class just to run `refresh` command? How is this dirty fix with a lot of additional work better than just running `fresh` instead of `refresh`? – Alexey Mezenin Dec 31 '17 at 13:09
  • Although this comment was posted in 2017: I don't use it in migrations, where I'd agree it's bad practice. I used it in a DB Seeder during local development and testing. I needed to truncate a table during seeding which was the target of foreign keys of other tables. I think there it absolutely makes sense. – Worp Apr 17 '21 at 07:01