mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Deprecate AUTO identifier generator strategy
#6804
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 @greg0ire on GitHub (Aug 7, 2021).
This strategy allows to pick the preferred strategy given the current platform. It allows full portability because you are guaranteed to have a working strategy no matter which RDBMS you use.
The issue is that a given strategy might be optimal at some point in time, then letter better strategies emerge.
For instance, for PostgreSQL,
AUTOtranslates to using sequences, but, as mentioned in https://github.com/doctrine/dbal/issues/5614 , that might no longer be the best strategy.Since we cannot really release a new major version of the ORM every time this happens, I suggest we deprecate the
AUTOstrategy and let people explicitly pick one.The obvious drawback is that full portability is lost.
@derrabus commented on GitHub (Aug 8, 2021):
An alternative could be that we make the ID generator strategy mapping configurable. The defaults would remain as-is for eternity, but if someone started a new app, they could decide that
AUTOon Postgres means "identity columns" and not "sequences".@greg0ire commented on GitHub (Aug 8, 2021):
They would have to know about all this, though, wouldn't they? The scenario I fear might happen then is:
doctrine/ormfor making bad choices for you.@derrabus commented on GitHub (Aug 9, 2021):
Yes. In Symfony apps, we could solve this with a recipe. In general, we could deprecate not configuring this mapping, so that we can throw in 3.0 when AUTO is used without a configured strategy for the database that is used.
Maybe there's a better solution, I'm just brainstorming.
I don't know if for instance sequences on Postgres are really the inferior choice, tbh. The idea is to allow the dev to make that choice.
The
AUTOstrategy is really used a lot. It's currently the default and the reasonable choice if we want the application to be portable. Sacrificing it sounds like a massive burden for apps using the ORM.@beberlei commented on GitHub (Aug 9, 2021):
Postgres now also has an identity / autoincrement like ID, I talked with Sergei about it a while ago, but forgot what it was exactly. It might be necessary to add this to DBAL and then we could really think about removing AUTO and making this explicit. Good DX Idea!
@greg0ire commented on GitHub (Aug 9, 2021):
@beberlei I think it might be the syntax mentioned in https://github.com/doctrine/dbal/issues/5614
It does indeed not seem supported in the DBAL yet, so this is probably too early.
Note that there is already some sort of support for autoincrement with the use of
SERIALthought (which is just a shortcut to a sequence, likeGENERATED ALWAYS | BY DEFAULT AS IDENTITYseems to be)?How so? I think if we implement the above in Postgres, then switching to
IDENTITYwould make the app fully portable, I think it would just be not supported on Oracle (which seems in fact wrong nowadays too)?@morozov commented on GitHub (Aug 9, 2021):
Would it be possible to introduce an API where the consumer could actually prefer the way to generate identity values if the target platform supports both? The defaults in ORM 2.x could correspond to the logic of the
prefers*methods in DBAL 2.x. The actualAUTOstrategy would still exist but would fail if more than one method is available for the current platform (hence, requiring user intervention). If a consumer changes their preference, they are responsible for the migration.In ORM 3.0, the defaults could be removed.
@greg0ire commented on GitHub (Aug 9, 2021):
I don't understand that part. Since the ORM would have defaults, the strategy would know what method to pick, wouldn't it? Or are you talking about ORM 3 where there would be no defaults?
@morozov commented on GitHub (Aug 9, 2021):
The
AUTOstrategy may exist even in ORM 3 but it would have to require user preferences for those platforms that support multiple strategies. This way, the application remains portable with little to no configuration (depending on whether the platforms supporting multiple strategies are used).@greg0ire commented on GitHub (Aug 9, 2021):
Sounds like a good plan. That way, Symfony application could be configured with a recipe to have "auto" mean "identity", and only newly-created apps would be affected.
Configuration is accessible from the CMF where we need to intervene:
4fa2f6baa4/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php (L93)@derrabus @beberlei what do you think?
@greg0ire commented on GitHub (Aug 10, 2021):
Meanwhile, I created an issue on the DBAL to talk about identity columns: https://github.com/doctrine/dbal/issues/4744
@beberlei commented on GitHub (Aug 15, 2021):
@greg0ire its a good idea! We would have a new configuration option accessible as
Configuration::get/setIdentityGenerationAutoStrategy($name)(better name TBD) and use defaults for each platform. Then users can change the strategies implementation.@greg0ire commented on GitHub (Aug 15, 2021):
Ok, so if somebody uses
AUTOand PostgreSQL, they would get deprecation messages telling them thatAUTOis defaulting toSEQUENCE, and that they:Configuration::setIdentityGenerationAutoStrategy([PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_SEQUENCE])explicitly;Configuration::setIdentityGenerationAutoStrategy([PostgreSQLPlatform::class => ClassMetadata::GENERATOR_TYPE_IDENTITY]).there would be a map called
$backwardsCompatibleDefaultsthat would stay the same forever, and another called$recommendedDefaultsthat would be used in the deprecation message above.setIdentityGenerationPreference()?@greg0ire commented on GitHub (Aug 20, 2021):
I think the vast majority of platforms support several strategies (
NONEorUUIDcome to mind). I now the the right think to do would be to deprecate reliance on the defaults iff those diverge from what we currently recommend, and I think we should recommendIDENTITYno matter the platformfor all platforms except Oracle.@morozov commented on GitHub (Aug 21, 2021):
For the end-user of the ORM, whether it's an identity column or a sequence-based implementation, the result is the same: the value in the column is auto-incremented. Why should they bother about the implementation details? This is different than say UUID where the resulting generated value is psudo-random.
What I'm saying is, we could probably rethink the granularity of this API and not expose
IDENTITY/SEQUENCEas two different values. Those are just platform-specific implementation details.@greg0ire commented on GitHub (Aug 21, 2021):
If I believe https://github.com/doctrine/dbal/issues/5614 , the user might want to use the ORM in some scenarios, and use SQL in others (to perform inserts), and then it becomes more cumbersome to use a sequence than to use SERIAL.
If we do that, how will the user control if a SEQUENCE or SERIAL or in the future,
GENERATED BY DEFAULT AS IDENTITYis used? Shouldn't they be able to specify it somehow? It's not only platform-specific if a platform supports multiple methods. If it does, it means either we have to take a decision and stick to it forever, or put the control in the hands of the user.@morozov commented on GitHub (Aug 21, 2021):
It looks like this discussion heavily overlaps with https://github.com/doctrine/orm/issues/8850 (specifically, https://github.com/doctrine/orm/issues/8850#issuecomment-903130348). I agree that SERIAL should be preferred to use if it's available on a given platform since this is eventually what users want. But if the platform only supports sequences, proper implementation will make it indistinguishable from
SERIALboth at the ORM and SQL levels. That's why I'm saying that at the end, the end-user shouldn't care about the implementation.@greg0ire commented on GitHub (Aug 22, 2021):
@morozov are you saying that what I have done in https://github.com/doctrine/orm/pull/8931 for
AUTOshould also be done forIDENTITY, and that we should removeSEQUENCE? Meaning when not configuredIDENTITYwould always result in the situation we have right now, but that the user could configure it to meanSERIALorSEQUENCEorGENERATED BY DEFAULT AS IDENTITY?@morozov commented on GitHub (Aug 24, 2021):
I probably confused
SERIALforIDENTITYin the context of PostgreSQL above.As an ORM-level API, yes. If a user chooses
IDENTITY, the ORM will use either identity an column, if supported by the target platform, or a sequence. Whether to name thisIDENTITYorAUTOINCREMENTit's up to the ORM but the latter seems to be more applicable. Whether it's an identity column or a sequence is a platform-specific implementation detail.At least as an upgrade path, yes. Eventually, the ORM could just choose the default implementation for the platform.
@stof commented on GitHub (Dec 4, 2021):
To me,
AUTOhas a nice benefit: it is something that can be used on all platforms (as the ORM takes care of choosing a supported id generator). this is especially useful when writing open-source packages (a bunch of Symfony bundles are shipping with entity mapping) where the platform is not known by the developer of the bundle (and so they intent their mapping to work on all platforms supported by the ORM)@stof commented on GitHub (Dec 4, 2021):
Note that if
IDENTITYis the one working on all platforms by switching to sequence internally, this is also fine for that case@norkunas commented on GitHub (Dec 21, 2023):
In one project I'm switching from MySQL to Postgres and now getting this deprecation, but none of my entities use generated ID's so a little bit strange to get this deprecation :)
@Mepcuk commented on GitHub (Jan 5, 2024):
I have the same deprication, but how to fix it?
@adrienbrault commented on GitHub (Jan 23, 2024):
Hey, I just ran into the following deprecation in a new app:
To fix the deprecation, I used a compiler pass to call
setIdentityGenerationPreferences:@madonzy commented on GitHub (May 8, 2024):
@adrienbrault thanks it helped, but actually in my case, I could avoid it by updating DBAL to version 4:
composer require doctrine/dbal:^4@linuxprocess commented on GitHub (Sep 27, 2024):
I fixed it in my entity's files by replacing :
#[ORM\GeneratedValue]with the recommanded :
#[ORM\GeneratedValue(strategy: "SEQUENCE")]@tacman commented on GitHub (Jan 27, 2025):
Is it possible to put the class name in the deprecation warning? I can't see to find the offending code.