DDC-1388: ORMPurger truncate fails for referenced tables in MySQL #1737

Closed
opened 2026-01-22 13:23:45 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 22, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user bubblez:

Running the doctrine:fixtures:load command in symfony2 invokes the doctrine ORMPurger, in my case for the MySQL platform, and throws following PDO Exception:

SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (sf2blog.comment, CONSTRAINT FK*9474526CDAE07E97 FOREIGN KEY (blog*id) REFERENCES sf2blog.blog (id))

That is because my comment table references the id column of the blog table and doctrine tries to truncate the blog table. To solve that you should run
SET foreign_key_checks=0;
prior to
TRUNCATE TABLE blog
and reactivate foreign key checks again
SET foreign_key_checks=0;

One way to achieve that would be to implement the getTruncateTableSQL method in the Doctrine\DBAL\Platforms\MySqlPlatform class.

Originally created by @doctrinebot on GitHub (Sep 22, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user bubblez: Running the doctrine:fixtures:load command in symfony2 invokes the doctrine ORMPurger, in my case for the MySQL platform, and throws following PDO Exception: SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`sf2blog`.`comment`, CONSTRAINT `FK*9474526CDAE07E97` FOREIGN KEY (`blog*id`) REFERENCES `sf2blog`.`blog` (`id`)) That is because my comment table references the id column of the blog table and doctrine tries to truncate the blog table. To solve that you should run SET foreign_key_checks=0; prior to TRUNCATE TABLE `blog` and reactivate foreign key checks again SET foreign_key_checks=0; One way to achieve that would be to implement the getTruncateTableSQL method in the Doctrine\DBAL\Platforms\MySqlPlatform class.
admin added the Bug label 2026-01-22 13:23:45 +01:00
admin closed this issue 2026-01-22 13:23:46 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 22, 2011):

Comment created by bubblez:

Added a patch with the changes suggested in the description.

@doctrinebot commented on GitHub (Sep 22, 2011): Comment created by bubblez: Added a patch with the changes suggested in the description.
Author
Owner

@doctrinebot commented on GitHub (Oct 10, 2011):

Comment created by @beberlei:

This was fixed by going back to delete by using a commandline switch. I need to check for a better fix.

@doctrinebot commented on GitHub (Oct 10, 2011): Comment created by @beberlei: This was fixed by going back to delete by using a commandline switch. I need to check for a better fix.
Author
Owner

@doctrinebot commented on GitHub (Oct 10, 2011):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 10, 2011): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/a710b259ee337f7fbda2

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/a710b259ee337f7fbda2 - [11065_fix_DDC-1388.patch](https://gist.github.com/a710b259ee337f7fbda2#file-11065_fix_DDC-1388-patch)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1737