Im trying to roll back my migrations.
My migrations file uses foreign keys like so
$table->foreign(\'user_one\')->references(\'id\')->on(\'u
I think this is the most correct approach:
public function down()
{
Schema::table('[table]', function (Blueprint $table) {
$table->dropForeign('[table]_[column]_foreign');
$table->dropColumn('[column]');
});
}
I think this is a better way to do it:
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('tableName');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
pm_convo_replys
has a foreign key that references pm_convo
, thus you cannot delete pm_convo
first without violating a foreign key constraint in pm_convo_replys
.
To delete both you need to delete pm_convo_replys
first.
public function down()
{
Schema::drop('pm_convo_replys');
Schema::drop('pm_convo');
}
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
prefer to do it this way
Schema::dropIfExists('tableNameChild');
Schema::drop('tableNameParents');
I also faced these kind of issues. Migration file order is the main issue here. The best way is to create migration files one by one. Main entities should be created first. Migration should be refreshed with every migrate file creation. (with php artisan migrate:refresh
)
According to @abkrim and @Eric
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::drop('tableName');
Schema::enableForeignKeyConstraints();
}