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.