[英]Getting cannot truncate a table referenced in a foreign key constraint in Yii framework
I'm getting cannot truncate a table referenced in a foreign key constraint in Yii framework 我得到的不能截断Yii框架中外键约束中引用的表
I have tried the below which I could see in some of the answers but still not working 我已经尝试了以下,我可以在一些答案中看到,但仍然无法正常工作
Yii::app()->db->createCommand('set foreign_key_checks=0')->execute();
$this->db->createCommand()->checkIntegrity(false)->execute();
$this->getFixtureManager()->checkIntegrity(false);
I get the error as below 我得到如下错误
Database Exception – yii\\db\\Exception 数据库异常 - yii \\ db \\ Exception
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint ( yiibasic
. educations
, CONSTRAINT FK_user
FOREIGN KEY ( user_id
) REFERENCES yiibasic
. users
( id
)) The SQL being executed was: TRUNCATE TABLE users
SQLSTATE [42000]:语法错误或访问冲突:1701无法截断外键约束引用的表( yiibasic
。 educations
,约束FK_user
外键( user_id
)参考yiibasic
。 users
( id
正在执行的SQL))为:截断表users
Error Info: Array ( 错误信息:数组(
[0] => 42000 [0] => 42000
[1] => 1701 [1] => 1701
[2] => Cannot truncate a table referenced in a foreign key constraint [2] =>无法截断外键约束中引用的表
( yiibasic
. educations
, CONSTRAINT FK_user
FOREIGN KEY ( user_id
) REFERENCES yiibasic
. users
( id
)) ) ( yiibasic
。 educations
,约束FK_user
外键( user_id
)参考yiibasic
。 users
( id
)))
Can any one help me to correct it. 任何人都可以帮我纠正它。 Also can you give the best way of seeding data in Yii I coudn't get a good tutorial for the same 你也可以在Yii中给出播种数据的最好方法我也不能得到一个好的教程
You must first disable the constraints : SET foreign_key_checks = 0;
您必须先禁用约束: SET foreign_key_checks = 0;
then truncate your tables 然后截断你的表
Then reactivate the constraints : SET foreign_key_checks = 1;
然后重新激活约束: SET foreign_key_checks = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.