Rework sequence-based identity generation #6791

Closed
opened 2026-01-22 15:38:44 +01:00 by admin · 5 comments
Owner

Originally created by @morozov on GitHub (Jul 18, 2021).

Originally assigned to: @greg0ire on GitHub.

Sequence-based identity values will be deprecated in DBAL 3.2.0 (https://github.com/doctrine/dbal/pull/4688) and removed in 4.0.x (https://github.com/doctrine/dbal/pull/4691). See the details in https://github.com/doctrine/dbal/issues/4687.

The ORM currently uses this API: https://github.com/doctrine/orm/blob/f3e55fae9fdbdbc23897006bdbf016c20e11f6e9/lib/Doctrine/ORM/Id/IdentityGenerator.php#L52-L55

Please consider not relying on this API.

Originally created by @morozov on GitHub (Jul 18, 2021). Originally assigned to: @greg0ire on GitHub. Sequence-based identity values will be deprecated in DBAL `3.2.0` (https://github.com/doctrine/dbal/pull/4688) and removed in `4.0.x` (https://github.com/doctrine/dbal/pull/4691). See the details in https://github.com/doctrine/dbal/issues/4687. The ORM currently uses this API: https://github.com/doctrine/orm/blob/f3e55fae9fdbdbc23897006bdbf016c20e11f6e9/lib/Doctrine/ORM/Id/IdentityGenerator.php#L52-L55 Please consider not relying on this API.
admin closed this issue 2026-01-22 15:38:44 +01:00
Author
Owner

@greg0ire commented on GitHub (Aug 21, 2021):

@beberlei I think this should go in a "DBAL 4" support project, shouldn't it? 😄

@greg0ire commented on GitHub (Aug 21, 2021): @beberlei I think this should go in a "DBAL 4" support project, shouldn't it? :smile:
Author
Owner

@morozov commented on GitHub (Aug 21, 2021):

In fact, this functionality in the ORM could be salvaged, if necessary. I updated the subject and the description.

The DBAL doesn't support using sequence values to implement lastInsertId() because it's not thread-safe but the sequences themselves are. The ORM could generate the identity value using a sequence explicitly and pass to the INSERT w/o relying on the lastInsertId() API in the same way as it does with UUIDs, for example:

$id = $conn->execute('SELECT ' . $seqName . '.NEXTVAL FROM DUAL');
$conn->insert($table, ['id' => $id]);

In this case, the ORM should not declare the column as autoincrement and should own the sequence, unlike the current implementation when the DBAL owns it.

It is also possible to leave the sequence ownership to the DBAL and have the column always auto-incremented via a trigger. In this case, the ORM should use a query like:

INSERT INTO table VALUES (...) RETURNING id INTO :id;

And then use an out parameter to get the id.

@morozov commented on GitHub (Aug 21, 2021): In fact, this functionality in the ORM could be salvaged, if necessary. I updated the subject and the description. The DBAL doesn't support using sequence values to implement `lastInsertId()` because it's not thread-safe but the sequences themselves are. The ORM could generate the identity value using a sequence explicitly and pass to the `INSERT` w/o relying on the `lastInsertId()` API in the same way as it does with UUIDs, for example: ```php $id = $conn->execute('SELECT ' . $seqName . '.NEXTVAL FROM DUAL'); $conn->insert($table, ['id' => $id]); ``` In this case, the ORM should _not_ declare the column as `autoincrement` and should own the sequence, unlike the current implementation when the DBAL owns it. It is also possible to leave the sequence ownership to the DBAL and have the column always auto-incremented via a trigger. In this case, the ORM should use a query like: ```sql INSERT INTO table VALUES (...) RETURNING id INTO :id; ``` And then use an out parameter to get the id.
Author
Owner

@stof commented on GitHub (Dec 4, 2021):

This is about using a SequenceGenerator instead of a IdentityGenerator then (as that's what the SequenceGenerator does)

@stof commented on GitHub (Dec 4, 2021): This is about using a SequenceGenerator instead of a IdentityGenerator then (as that's what the SequenceGenerator does)
Author
Owner

@stof commented on GitHub (Dec 4, 2021):

I suggest instead deprecating the support for sequence-based IdentityGenerator, so that projects using a platform that does not support identity columns have to use something else (for instance the SequenceGenerator)

@stof commented on GitHub (Dec 4, 2021): I suggest instead deprecating the support for sequence-based IdentityGenerator, so that projects using a platform that does not support identity columns have to use something else (for instance the SequenceGenerator)
Author
Owner

@onurkose commented on GitHub (Aug 30, 2023):

Hello, today I changed the id columns' strategy from IDENTITY to SEQUENCE in my entities after reading the deprecation messages, and now all the insert statements have null id field values if the entity is using JOINED type entity inheritance.

setIdentifierValues method of ClassMetadataInfo class sets the new ids to reflection class, but then it turns into null somehow.

@onurkose commented on GitHub (Aug 30, 2023): Hello, today I changed the id columns' strategy from IDENTITY to SEQUENCE in my entities after reading the deprecation messages, and now all the insert statements have `null` id field values if the entity is using JOINED type entity inheritance. `setIdentifierValues` method of `ClassMetadataInfo` class sets the new ids to reflection class, but then it turns into `null` somehow.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6791