Skip to content

Using lastval() instead of currval('sequence_name') for fetching IDs can return wrong ID #11880

Open
@Brajk19

Description

@Brajk19

In ORM 2, newly inserted IDs in Postgres were fetched using:

SELECT currval('sequence_name')

However, in Doctrine ORM 3, this has been changed to:

SELECT lastval()

This change introduces an issue when using tools like pg_repack (or triggers in general), which create triggers that make insert into log table while doing table repack. These inserts cause another sequence to increment, meaning that lastval() may return an ID from the wrong sequence.

I understand that this change was made because of issues with Oracle (doctrine/dbal#4687) but i I think it would be useful if this behaviour could be modified.
For Postgres it makes more sense to use currval('sequence_name').

Even having option to define our own generator when using #[ORM\GeneratedValue(strategy: 'IDENTITY')] would be useful.
Currently it depends on type so only IdentityGenerator and BigIntegerIdentityGenerator can be used:

$generator = $fieldName && $class->fieldMappings[$fieldName]->type === 'bigint'
? new BigIntegerIdentityGenerator()
: new IdentityGenerator();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions