PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Getting cannot truncate a table referenced in a foreign key constraint in Yii framework

 February 16, 2022     php, yii     No comments   

Issue

I'm getting cannot truncate a table referenced in a foreign key constraint in Yii framework

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

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

Error Info: Array (
[0] => 42000
[1] => 1701
[2] => Cannot truncate a table referenced in a foreign key constraint
(yiibasic.educations, CONSTRAINT FK_user FOREIGN KEY (user_id) REFERENCES 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


Solution

You must first disable the constraints : SET foreign_key_checks = 0;

then truncate your tables

Then reactivate the constraints : SET foreign_key_checks = 1;



Answered By - loustalet
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing