I'm having a problem with migration on my Laravel project.
Since i'm fairly new to Laravel I can't figure it out.
I want to add a foreign key to an already existing table and this works, but when I refresh my migrations I get this error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails (SQL: drop table `battles`)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key
constraint fails
These are the migrations I currently have:
Table Projects
class CreateProjectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->string('tags');
$table->string('img');
$table->string('img_tricolor');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('projects');
}
}
Table Battles
class CreateBattlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('battles', function (Blueprint $table) {
$table->increments('id');
$table->string('battle_theme');
$table->boolean('battle_active');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('battles');
}
}
Adding foreign key for battles in projects
class AddProjectsBattleIdFk extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('projects', function (Blueprint $table) {
$table->integer('battle_id')->unsigned();
$table->foreign('battle_id')->references('id')->on('battles')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('projects', function (Blueprint $table) {
//
});
}
}
I suppose it has something to do with the battles table.
In down methods you need to remove foreign keys first:
In CreateProjectsTable
public function down()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign('projects_user_id_foreign');
});
Schema::drop('projects');
}
In AddProjectsBattleIdFk
public function down()
{
Schema::table('projects', function (Blueprint $table) {
$table->dropForeign('projects_battle_id_foreign');
$table->dropColumn('battle_id');
});
}
I had the same problem,the Marcin's answer worked but I also found that another option is using fresh instead of refresh, in that case you don't need to remove foreign keys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With