SQLSTATE [23000]:违反完整性约束:1217 [英] SQLSTATE[23000]: Integrity constraint violation: 1217
问题描述
在我的项目中进行了多次迁移之后,我想回滚以更新一些内容,但是当我尝试删除 projects
表时突然出现了此错误.我仔细检查了外键约束,但找不到错误.
After creatin multiple migrations in my project, I wanted to rollback to update a few things but suddenly I got this error when I tried to drop projects
table. I double checked the foreign key constrains but I can't find the error.
[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.创建表用户:
Here are my migrations: 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');
}
在上一次迁移中,我可能正在做的错误是什么?!迁移时我没有遇到任何麻烦,但仅当我回滚以添加任何更改时才会出现.
In the last migration, what could possibly be the mistake I am doing?! I face no trouble when migrating but it appears only when I rollback to add any changes.
知道为什么会这样吗?
推荐答案
如果上面仅提到3个表,那么您的迁移就没有问题,因为我自己用
If there are only 3 tables above mentioned, there is no issue in your migration as I have tried it myself with
php artisan migrate
创建表和
php artisan migrate:rollback
要回滚.
我知道的一件事是Laravel将基于迁移文件时间戳假定迁移的顺序.
One thing that I know is Laravel is going to assume the sequence of the migration based on the migration file timestamp.
因此,我非常确定还有另一个表,该表具有对 projects
表的外键引用,该表尚未因错误消息而被删除(SQL:drop table projects
)
Therefore, I am quite sure there is another table that has a foreign key reference to the projects
tables that has not been dropped as the error messsage is (SQL: drop table projects
)
尝试使用 php artisan tinker
,然后使用 Schema :: drop('some_table_name');
,其中 some_table_name
是具有参考的表到 projects
表,然后再次删除 projects
表.
Try to use php artisan tinker
and then Schema::drop('some_table_name');
where some_table_name
is the table that has reference to projects
table, then drop the projects
table again.
这篇关于SQLSTATE [23000]:违反完整性约束:1217的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!