我在我的 Laravel 项目中遇到了迁移问题。
由于我对 Laravel 还很陌生,所以我无法弄清楚。
我想向一个已经存在的表添加一个外键,这可以工作,但是当我刷新我的迁移时,我得到了这个错误:
[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
这些是我目前拥有的迁移:
表项目
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');
}
}
桌上对战
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');
}
}
为项目中的战斗添加外键
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) {
//
});
}
}
我想这与战斗表有关。