Laravel 5.1 Migration and Seeding Cannot truncate a table referenced in a foreign key constraint

前端 未结 9 2156
借酒劲吻你
借酒劲吻你 2020-12-15 05:04

I\'m trying to run the migration (see below) and seed the database, but when I run

php artisan migrate --seed

I get this error:

<         


        
9条回答
  •  悲&欢浪女
    2020-12-15 05:17

    Here is what works for me every time. When you're adding the foreign key, make sure to add cascade. the syntax is like this

    $table->foreign('column')->references('id')->on('table_name')->onDelete('cascade');
    

    Make sure to replace id with whatever field is applicable for you.

    Now before running the seeding add this instead of trucate

    DB::statement('DELETE FROM table_name');
    

    It will delete all the data. Hope this helps.

提交回复
热议问题