DBAL-479: Doctrine2 schema-tool:update doesn't re-create foreign keys on entity change #1679

Closed
doctrinebot opened this Issue Jan 7, 2012 · 9 comments

2 participants

@doctrinebot

Jira issue originally created by user felicitus:

I have changed a @ManyToOne relation to another entity. My workflow was this:

  • Having @ManyToOne(targetEntity="Entity\EntityA") in the source
  • Creating the database schema
  • Changing @ManyToOne(targetEntity="Entity\EntityA") to @ManyToOne(targetEntity="Entity\EntityB")
  • Calling doctrine orm:schema-tool:update

orm:schema-tool:update doesn't notice that and doesn't re-create the FK on MySQL and PostgreSQL (haven't tested others).

@doctrinebot

Comment created by @beberlei:

Does this use the same variable or does the many to one variable name change?

@doctrinebot

Comment created by felicitus:

Yes, same variable name. For the real-world change, see:

partkeepr/PartKeepr@1cf520f

@doctrinebot

Comment created by felicitus:

Any news of this issue?

This is actually not a duplicate of DDC-1585.

@doctrinebot

Comment created by felicitus:

13:32:46 < beberlei> it is a DBAL issue, so you should start replicating it there
13:33:10 < beberlei> i mean, have a look at how the DBAL SchemaDiff looks like in that case, and why it creates the sql wrong
13:33:19 < beberlei> probably the comperator misses something

@doctrinebot

Comment created by felicitus:

I finally found the spot where the check is missing:

Comparator::diffForeignKey misses to compare the foreign table name, which can be added as this:

if ($key1->getForeignTableName() != $key2->getForeignTableName()) {
       return true;
}
@doctrinebot

Comment created by jsmitty:

I've looked through the bugs in the DBAL project that reference the Comparator and I can't find one for this issue.

Has one been created for it that I have missed?

Also, I tried adding the suggested code into my project and schema-tool:update generated SQL to drop and recreate every foreign key relationship. I applied those changes and ran schema-tool:update again, expecting to get the normal "nothing to update" message. What I got was SQL to drop and recreate every foreign key relationship. Again.

Tracing the code revealed that (in one case) $key1->getForeignTableName() was returning 'user' and $key2->getForeignTableName() was returning 'User'. All of the foreign key comparisons followed the same pattern. I applied strtolower() to each of them before the comparison and now things seem to behave as I expect, but I don't know how portable my solution is.

if (strtolower($key1->getForeignTableName()) != strtolower($key2->getForeignTableName())) {
    return true;
}

I think applying strtolower() like this is OK since something similar is done when comparing local columns and foreign columns earlier in Comparator::diffForeignKey().

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by @beberlei:

This was fixed in d7908fe and is part of Doctrine 2.3

@doctrinebot doctrinebot added the Bug label Dec 6, 2015
@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.3 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment