在我的项目中创建多个迁移后,我想回滚以更新一些内容,但是当我尝试删除projects
表时突然出现此错误。我仔细检查了外键约束,但找不到错误。
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or upda
te a parent row: a foreign key constraint fails (SQL: drop table `projects`
)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or upda
te a parent row: a foreign key constraint fails
这是我的迁移: 1.create table users:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email', 50)->unique();
$table->string('password', 60);
$table->string('password_temp', 60);
$table->string('code', 60);
$table->boolean('active');
$table->string('remember_token', 100);
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
2.创建客户表:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::drop('clients');
}
3.创建项目表:
public function up()
{
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('status');
$table->integer('client_id')->unsigned()->index()->nullable();
$table->foreign('client_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::drop('projects');
}
在上一次迁移中,我可能犯的错误是什么?!迁移时我没有遇到任何问题,但只有在我回滚以添加任何更改时才会出现。
知道为什么会这样吗?