AutoIncrement pgSQL without identity strategy #7184

Closed
opened 2026-01-22 15:46:16 +01:00 by admin · 2 comments
Owner

Originally created by @vencakrecl on GitHub (Jul 12, 2023).

Bug Report

The AUTO and SEQUENCE strategy does not support autoincrement, only 'IDENTITY' works.

Q A
BC Break no
Version 2.15.3

Summary

Current behavior

IDENTITY

        $this->addSql('CREATE SEQUENCE billing_plan_id_seq');
        $this->addSql('SELECT setval(\'billing_plan_id_seq\', (SELECT MAX(id) FROM billing_plan))');
        $this->addSql('ALTER TABLE billing_plan ALTER id SET DEFAULT nextval(\'billing_plan_id_seq\')');

AUTO, SEQUENCE

        $this->addSql('CREATE SEQUENCE billing_plan_id_seq');

How to reproduce

    #[ORM\Id]
    #[ORM\Column(type: Types::INTEGER, unique: true, nullable: false)]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    private ?int $id = null;
    #[ORM\Id]
    #[ORM\Column(type: Types::INTEGER, unique: true, nullable: false)]
    #[ORM\GeneratedValue(strategy: 'AUTO')] // #[ORM\GeneratedValue(strategy: 'SEQUENCE')
    private ?int $id = null;

Expected behavior

IDENTITY, AUTO, SEQUENCE

        $this->addSql('CREATE SEQUENCE billing_plan_id_seq');
        $this->addSql('SELECT setval(\'billing_plan_id_seq\', (SELECT MAX(id) FROM billing_plan))');
        $this->addSql('ALTER TABLE billing_plan ALTER id SET DEFAULT nextval(\'billing_plan_id_seq\')');
Originally created by @vencakrecl on GitHub (Jul 12, 2023). ### Bug Report The `AUTO` and `SEQUENCE` strategy does not support autoincrement, only 'IDENTITY' works. | Q | A |------------ | ------ | BC Break | no | Version | 2.15.3 #### Summary #### Current behavior IDENTITY ``` $this->addSql('CREATE SEQUENCE billing_plan_id_seq'); $this->addSql('SELECT setval(\'billing_plan_id_seq\', (SELECT MAX(id) FROM billing_plan))'); $this->addSql('ALTER TABLE billing_plan ALTER id SET DEFAULT nextval(\'billing_plan_id_seq\')'); ``` AUTO, SEQUENCE ``` $this->addSql('CREATE SEQUENCE billing_plan_id_seq'); ``` #### How to reproduce ``` #[ORM\Id] #[ORM\Column(type: Types::INTEGER, unique: true, nullable: false)] #[ORM\GeneratedValue(strategy: 'IDENTITY')] private ?int $id = null; ``` ``` #[ORM\Id] #[ORM\Column(type: Types::INTEGER, unique: true, nullable: false)] #[ORM\GeneratedValue(strategy: 'AUTO')] // #[ORM\GeneratedValue(strategy: 'SEQUENCE') private ?int $id = null; ``` #### Expected behavior IDENTITY, AUTO, SEQUENCE ``` $this->addSql('CREATE SEQUENCE billing_plan_id_seq'); $this->addSql('SELECT setval(\'billing_plan_id_seq\', (SELECT MAX(id) FROM billing_plan))'); $this->addSql('ALTER TABLE billing_plan ALTER id SET DEFAULT nextval(\'billing_plan_id_seq\')'); ```
admin closed this issue 2026-01-22 15:46:18 +01:00
Author
Owner

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

When it set to AUTO and if the platform is pgSql, it's set to SEQUENCE.
Then, before the insert query, it runs such query to calculate next id and puts into the INSERT INTO statement;
SELECT NEXTVAL('table_id_seq')

This is fine so far and it works for me if the entity doesn't use any inheritance mapping.
But it's always null when I try to create a new record with these entities that uses inheritance.
What's your case?

@onurkose commented on GitHub (Aug 30, 2023): When it set to `AUTO` and if the platform is pgSql, it's set to `SEQUENCE`. Then, before the insert query, it runs such query to calculate next id and puts into the `INSERT INTO` statement; `SELECT NEXTVAL('table_id_seq')` This is fine so far and it works for me if the entity doesn't use any inheritance mapping. But it's always null when I try to create a new record with these entities that uses inheritance. What's your case?
Author
Owner

@vencakrecl commented on GitHub (Oct 3, 2023):

The SEQUENCE without default value does not make sense because you can insert any value into the ID column and break the sequence. The second thing is the sequence does not respect the last ID and begins from 1.

@vencakrecl commented on GitHub (Oct 3, 2023): The `SEQUENCE` without default value does not make sense because you can insert any value into the ID column and break the sequence. The second thing is the sequence does not respect the last ID and begins from 1.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7184