mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
UuidGenerator attribute is ignored by Doctrine #7123
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 @kgrozdanovski on GitHub (Mar 29, 2023).
Bug Report
Summary
UuidGenerator attribute is ignored
Current behavior
When persisting an entity with a UUID type for the
idproperty, as per Symfony's and Doctrine's documentation, Doctrinefails to identify the built-in UuidGenerator and defaults to attempting to create a sequential ID (since the database column is defined as
uuidin Postgres the query ultimately fails since it cannot find<table>_id_seq.How to reproduce
Doctrine ORM version
2.14.1PHP version
8.2Postgresql for Docker, image tag
postgres:15The above versions are provided for context however considering the following analysis they seem to be irrelevant.
My entity property definition:
Additionally I have attempted to use
strategy: 'CUSTOM'though that yielded the same result.I have traced this problem to a missing 'instance' property, which is only ever defined by the
MappingDriver::loadMetadataForClass()method, however this method is never called since theAttributeDriverclass is used instead, and in that class only the 'class' property is set but no the 'instance' property:Compared to the MappingDriver from doctrine-bundle:
This
instanceproperty is referenced in theClassMetadataFactoryclass from doctrine-bundle:Since this method returns void, the custom ID generator (
UuidGeneratorin this case) is never set.Expected behavior
The ORM should invoke the
gen_random_uuid()database function to generate a UUID value.@greg0ire commented on GitHub (Mar 29, 2023):
There might be a bug in the attribute driver, but please note that the
UuidGeneratoris deprecated since https://github.com/doctrine/orm/pull/8813See also: https://github.com/doctrine/dbal/pull/3212
@kgrozdanovski commented on GitHub (Mar 29, 2023):
Thanks for the prompt reply.
Despite Doctrine's
UuidGeneratorbeing deprecated this would still seem to be a problem for all custom generators defined via PHP attributes.@kgrozdanovski commented on GitHub (Apr 1, 2023):
After further debugging I traced the problem to another property being declared with the
#[ORM\GeneratedValue]attribute (contained in another trait for common properties - e.g.date_created).Apparently if you have multiple
GeneratedValueattributes on different properties within an Entity class, they override the settypeand in my case the type of the Generated value was overwritten - Doctrine determined it should use a Sequential generator instead of the desired UUID generator.After removing the attribute from all properties except the ID this worked as expected.
In conclusion, I'm not sure if this is the intended behavior or not, but if it is, then I would recommend Doctrine throws a warning or exception when using multiple
GeneratedValueattributes with different types within the same class. If it isn't and if the intended behavior is to support multiple generated values with different generators (which makes more sense) then there is still a bug however it should be rephrased to "Different GeneratedValue types not supported/working)".I will leave the issue open until someone from the team decides what to do with this.
@derrabus commented on GitHub (Apr 2, 2023):
The ORM's UUID generator is not compatible with Symfony's UUID type. And I don't really think that we should make them compatible, tbh. Just generate new UUIDs in your application. That's what UUIDs are made for.