Using migrations to delete table with foreign key

后端 未结 7 860
不思量自难忘°
不思量自难忘° 2020-12-24 06:25

Im trying to roll back my migrations.

My migrations file uses foreign keys like so

$table->foreign(\'user_one\')->references(\'id\')->on(\'u         


        
7条回答
  •  隐瞒了意图╮
    2020-12-24 06:39

    Important, this is for Laravel 5.4.

    According to the docs

    To drop a foreign key, you may use the dropForeign method. Foreign key constraints use the same naming convention as indexes. So, we will concatenate the table name and the columns in the constraint then suffix the name with "_foreign"

    $table->dropForeign('posts_user_id_foreign');
    

    Or, you may pass an array value which will automatically use the conventional constraint name when dropping:

    $table->dropForeign(['user_id']);
    

    I personally prefer the second one because of simplicity

提交回复
热议问题