mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
MySQL: Wrong order of generated primary keys #5665
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @fxOne on GitHub (Sep 1, 2017).
I defined in my
address.orml.yml:Generates the following MySQL Code on the Symfony
doctrine:migration:diffcommand:I would expect that the primary key would be
user_id, address_typeas I defined it in this order.Is this a bug or do I must define something else to get the primary key in my expected order?
@Ocramius commented on GitHub (Sep 2, 2017):
This is possibly because associations are processed after normal fields: you will likely need to check the identifier field order in the
ClassMetadatainstance and try reproducing a test case from there.@Ocramius commented on GitHub (Sep 2, 2017):
Also, I think this might be an ORM bug, not a DBAL bug.
@raalderink commented on GitHub (Apr 29, 2022):
I just ran into the same issue, and this is definitely a DBAL bug. I traced in to the following piece of code (in doctrine/dbal 2,13,3, but the code is still the same in the latest version)
The
AbstractPlatform::getCreateTableSQL()does the following:getQuotedColumnsis this function:As you can see, this simply goes through the known columns, which is fields first, association keys second. It does not take into account the order of the fields listed in de primary key.
@beberlei commented on GitHub (Apr 29, 2022):
The columbs are put into the index in wrong order by the SchemaTool though. The order is defined in ClassMetadata in a property. Maybe YamlDriver parses the document weirdly. Is this also a problem with XmlDriver?
@raalderink commented on GitHub (Apr 29, 2022):
You are correct, we went down the rabbithole and this is indeed an issue in the YamlDriver. Where exactly we've not found out yet. Unsure about the XmlDriver.
@Yoshix commented on GitHub (Sep 1, 2022):
I can confirm that the XmlDriver has the same problem.
My guess is it's this line:
5d11648767/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php (L373)Out of curiosity, I added the following code at the end of that method:
With the corresponding xml tag in one of my mapping files:
And it "fixed" the problem.
@uncaught commented on GitHub (May 10, 2023):
I've found the problem in the YamlDriver: While it iterates over the
ids before thefields, it skips those withassociationKey: trueuntil after thefields.See
70477d81e9/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php (L334)This causes the association key ids to be added after the fields.
I've overridden the
loadMetadataForClassto fix the order to the one defined in the yaml. The ids of any base entity class won't be in the yaml though, so this has to be taken into account and I've also added anidOrderto override if the outcome is not desired when working with extending classes:@tehmaestro commented on GitHub (Nov 5, 2025):
Hey, I recently had to deal with this problem.
The way I fixed it is by adding a custom compiler pass that
because I wanted to keep the arguments for the definition the same, so I just switch the class in-place
Then, in my custom class I do: