New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncating table with foreign keys fails #17

Closed
ghost opened this Issue Jun 12, 2011 · 25 comments

Comments

Projects
None yet
@ghost

ghost commented Jun 12, 2011

When I am calling symfony CLI command:

php app/console doctrine:fixtures:load

I get

[PDOException] 

SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (symfony.param_product, CONSTRAINT param_product_ibfk_1 FOREIGN KEY (param_val_id) REFERENCES symfony.param_value (id))

Table symfony.param_product is created by @Orm\JoinTable annotation (with @Orm\ManyToMany). I am using MySQL

This worked fine before this change: 91ff6eb

I thought it might be bug on my side (e.g. no onDelete options) but everything I tried failed.

As far as I learned e.g. from http://forums.asp.net/t/1283840.aspx/1?How+can+I+truncate+the+table+which+have+foreign+key+

 ON DELETE CASCADE is true only for deleting records and not for truncating tables.

 You have to drop the foreign key constraint from Child Table that references the Master Table to be truncated, then after only you are able to truncate the Master Table. 

So it seems to me the only solution is to use DELETE or drop the foregin keys first.

Can we revert the 91ff6eb change to fix this problem? @beberlei?

@stof

This comment has been minimized.

Show comment
Hide comment
@stof

stof Jun 12, 2011

Member

The issue with using DELETE is that it does not reset the auto increment value

Member

stof commented Jun 12, 2011

The issue with using DELETE is that it does not reset the auto increment value

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jun 13, 2011

Ok, but the issue with TRUNCATE is that the doctrine:fixtures:load command just does NOT work.

If it's really needed, starting auto increment from 1 can be always done for example by dropping and recreating tables, right?

ghost commented Jun 13, 2011

Ok, but the issue with TRUNCATE is that the doctrine:fixtures:load command just does NOT work.

If it's really needed, starting auto increment from 1 can be always done for example by dropping and recreating tables, right?

@theinterned

This comment has been minimized.

Show comment
Hide comment
@theinterned

theinterned Jun 14, 2011

Yep - I've got this issue too. Rolling back to 91ff6eb for now.

theinterned commented Jun 14, 2011

Yep - I've got this issue too. Rolling back to 91ff6eb for now.

@kbond

This comment has been minimized.

Show comment
Hide comment
@kbond

kbond Jun 22, 2011

+1 for reverting 91ff6eb

kbond commented Jun 22, 2011

+1 for reverting 91ff6eb

@leahaense

This comment has been minimized.

Show comment
Hide comment
@leahaense

leahaense Jun 30, 2011

Contributor

Why not just add 'CASCADE' to the TRUNCATE? $platform->getTruncateTableSQL($tbl) has a second parameter that would return something like 'TRUNCATE table CASCADE' (at least for postgres it does) and with that it works.

Contributor

leahaense commented Jun 30, 2011

Why not just add 'CASCADE' to the TRUNCATE? $platform->getTruncateTableSQL($tbl) has a second parameter that would return something like 'TRUNCATE table CASCADE' (at least for postgres it does) and with that it works.

@beberlei

This comment has been minimized.

Show comment
Hide comment
@beberlei

beberlei Jun 30, 2011

Member

How about adding a pull request? I can merge it then.

Member

beberlei commented Jun 30, 2011

How about adding a pull request? I can merge it then.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jul 4, 2011

@leahaense

The issue still exists when using MySQL (even after 4a8464e).

ghost commented Jul 4, 2011

@leahaense

The issue still exists when using MySQL (even after 4a8464e).

@beberlei

This comment has been minimized.

Show comment
Hide comment
@beberlei

beberlei Jul 4, 2011

Member

In that case your foreign keys are bi-directional or something?

Member

beberlei commented Jul 4, 2011

In that case your foreign keys are bi-directional or something?

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jul 6, 2011

@beberlei yes I think so. I am not 100% sure as SQL is generated by Doctrine from one-to-many/many-to-many annotations (which yes are bi-directional).

Anyone else is having this issue on MySQL? Or is it just me?

ghost commented Jul 6, 2011

@beberlei yes I think so. I am not 100% sure as SQL is generated by Doctrine from one-to-many/many-to-many annotations (which yes are bi-directional).

Anyone else is having this issue on MySQL? Or is it just me?

@codecowboy

This comment has been minimized.

Show comment
Hide comment
@codecowboy

codecowboy Jul 7, 2011

Can you do foreign_key_checks=0 before TRUNCATE ?

http://bugs.mysql.com/bug.php?id=58788

I'm also hitting this issue

codecowboy commented Jul 7, 2011

Can you do foreign_key_checks=0 before TRUNCATE ?

http://bugs.mysql.com/bug.php?id=58788

I'm also hitting this issue

@srosato

This comment has been minimized.

Show comment
Hide comment
@srosato

srosato Jul 13, 2011

I've ran with the exact same issue with my partner, this does happen on MySQL 5.5 while 5.1 is fine with truncating tables with foreign keys. Reverting back to DELETE wouldn't be an elegant solution as stated before with the auto_increment keys not resetting.

srosato commented Jul 13, 2011

I've ran with the exact same issue with my partner, this does happen on MySQL 5.5 while 5.1 is fine with truncating tables with foreign keys. Reverting back to DELETE wouldn't be an elegant solution as stated before with the auto_increment keys not resetting.

@dogbrain

This comment has been minimized.

Show comment
Hide comment
@dogbrain

dogbrain Jul 17, 2011

Would be nice if there could come a fix like foreign_key_checks=0 for mysql 5.5

http://bugs.mysql.com/bug.php?id=54678

dogbrain commented Jul 17, 2011

Would be nice if there could come a fix like foreign_key_checks=0 for mysql 5.5

http://bugs.mysql.com/bug.php?id=54678

@dogbrain

This comment has been minimized.

Show comment
Hide comment
@dogbrain

dogbrain Jul 17, 2011

Here is a quick dirty hack for getting it to work with MySQL 5.5

diff --git a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
index a580c1f..ff758c1 100644
--- a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
+++ b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
@@ -82,10 +82,12 @@ class ORMPurger implements PurgerInterface
             $orderedTables[] = $class->getTableName();
         }

+       $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 0;");
         $platform = $this->em->getConnection()->getDatabasePlatform();
         foreach($orderedTables as $tbl) {
             $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
         }
+       $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 1;");
     }

     private function getCommitOrder(EntityManager $em, array $classes)

dogbrain commented Jul 17, 2011

Here is a quick dirty hack for getting it to work with MySQL 5.5

diff --git a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
index a580c1f..ff758c1 100644
--- a/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
+++ b/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
@@ -82,10 +82,12 @@ class ORMPurger implements PurgerInterface
             $orderedTables[] = $class->getTableName();
         }

+       $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 0;");
         $platform = $this->em->getConnection()->getDatabasePlatform();
         foreach($orderedTables as $tbl) {
             $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
         }
+       $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 1;");
     }

     private function getCommitOrder(EntityManager $em, array $classes)
@yakobe

This comment has been minimized.

Show comment
Hide comment
@yakobe

yakobe Jul 22, 2011

The above fix works great for me. Any ideas when a fix could be introduced to repo so i dont have to change it manually all the time?

yakobe commented Jul 22, 2011

The above fix works great for me. Any ideas when a fix could be introduced to repo so i dont have to change it manually all the time?

@adrienbrault

This comment has been minimized.

Show comment
Hide comment
@adrienbrault

adrienbrault Jul 30, 2011

I also had to add this fix to use the bundle.

adrienbrault commented Jul 30, 2011

I also had to add this fix to use the bundle.

@mattsnowboard

This comment has been minimized.

Show comment
Hide comment
@mattsnowboard

mattsnowboard Aug 1, 2011

The above fixed it for me (as far as I can tell, still learning Doctrine/Symfony2)

mattsnowboard commented Aug 1, 2011

The above fixed it for me (as far as I can tell, still learning Doctrine/Symfony2)

@komputerwiz

This comment has been minimized.

Show comment
Hide comment
@komputerwiz

komputerwiz Aug 5, 2011

I made the foreign_key_checks changes to MySqlPlatform on the DBAL project itself just minutes after @mdarse submitted his pull request:

doctrine/dbal#42

komputerwiz commented Aug 5, 2011

I made the foreign_key_checks changes to MySqlPlatform on the DBAL project itself just minutes after @mdarse submitted his pull request:

doctrine/dbal#42

@frastel

This comment has been minimized.

Show comment
Hide comment
@frastel

frastel Aug 19, 2011

Same problem on my machine with MySQL 5.5.9, however dogbrain's hack helped.

frastel commented Aug 19, 2011

Same problem on my machine with MySQL 5.5.9, however dogbrain's hack helped.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Aug 29, 2011

Can we get this fix integrated please? Otherwise everyone always has to add it manually to get it working.

ghost commented Aug 29, 2011

Can we get this fix integrated please? Otherwise everyone always has to add it manually to get it working.

@beberlei

This comment has been minimized.

Show comment
Hide comment
@beberlei

beberlei Aug 29, 2011

Member

Its not a "fix" its a hack. I wont merge a hack.

Member

beberlei commented Aug 29, 2011

Its not a "fix" its a hack. I wont merge a hack.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Aug 29, 2011

Ok... fair enough. However, it's a hack that allows the fixtures to work and it would be nice if they did ;)
If this cant be merged, is there a fix that can be? Otherwise there is a bug that makes this bundle unusable for many people (without constantly manually hacking it's code every time an update is made)

ghost commented Aug 29, 2011

Ok... fair enough. However, it's a hack that allows the fixtures to work and it would be nice if they did ;)
If this cant be merged, is there a fix that can be? Otherwise there is a bug that makes this bundle unusable for many people (without constantly manually hacking it's code every time an update is made)

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Aug 29, 2011

@beberlei?

Can we revert the 91ff6eb change to fix this problem?

This way it won't be a hack. It was suggested in the first post in this issue.

ghost commented Aug 29, 2011

@beberlei?

Can we revert the 91ff6eb change to fix this problem?

This way it won't be a hack. It was suggested in the first post in this issue.

@beberlei beberlei closed this in 41ef09a Sep 25, 2011

@mconti80

This comment has been minimized.

Show comment
Hide comment
@mconti80

mconti80 Nov 30, 2012

I'm not sure I'm doing it right, however I have the same problem. I can't figure out what version I'm using, I've downloaded with composer with

"require": {
    "doctrine/doctrine-fixtures-bundle": "dev-master"
},

Anyway, in order to obtain an index reset, I need to run "doctrine:schema:drop --force" and then "doctrine:schema:update --force" before reloading. Otherwise I have the same error. Can someone pls tell me what I'm doing wrong?
Thanks in advance.

mconti80 commented Nov 30, 2012

I'm not sure I'm doing it right, however I have the same problem. I can't figure out what version I'm using, I've downloaded with composer with

"require": {
    "doctrine/doctrine-fixtures-bundle": "dev-master"
},

Anyway, in order to obtain an index reset, I need to run "doctrine:schema:drop --force" and then "doctrine:schema:update --force" before reloading. Otherwise I have the same error. Can someone pls tell me what I'm doing wrong?
Thanks in advance.

@orourkedd

This comment has been minimized.

Show comment
Hide comment
@orourkedd

orourkedd Feb 25, 2013

I'm having the same issue running the latest version of doctrine fixtures and Mysql 5.5. The "hack" worked, but that's not a portable solution. I have to drop the schema and recreate it every time.

orourkedd commented Feb 25, 2013

I'm having the same issue running the latest version of doctrine fixtures and Mysql 5.5. The "hack" worked, but that's not a portable solution. I have to drop the schema and recreate it every time.

@sergiu-popa

This comment has been minimized.

Show comment
Hide comment
@sergiu-popa

sergiu-popa Oct 27, 2016

Do yourself a favor and create a script inside Symfony folder, like load_fixtures:

bin/console doc:sc:drop --force
bin/console doc:sc:cr
bin/console doc:fix:lo --no-interaction

Run chmod 755 load_fixtures and then ./load_fixtures.

sergiu-popa commented Oct 27, 2016

Do yourself a favor and create a script inside Symfony folder, like load_fixtures:

bin/console doc:sc:drop --force
bin/console doc:sc:cr
bin/console doc:fix:lo --no-interaction

Run chmod 755 load_fixtures and then ./load_fixtures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment