migrations icon indicating copy to clipboard operation
migrations copied to clipboard

MariaDB to PostgreSQL -> Doctrine migration breaks ID sequence

Open signmeuptwice opened this issue 7 months ago • 9 comments

Q A
Doctrine 3 and DBAL 4

Support Question

Sorry for asking here but I have been on this for days and I cannot find a solution.

I have been migrating from MySQL to PostgreSQL

I import my old MariaDB data into Postgre using PGLoader and my app works just fine right after import. Please note that at this point there are no id_seq files generated BUT my app works just fine. I can add new entries without error.

here is a screen of id column after pgloader Screenshot 2024-07-24 at 08 02 55

All my entities are like so on the id column:

    #[ORM\Column(name: 'id', type: 'integer', nullable: false)]
    #[Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    #[Groups(['ad'])]
    private $id;

I now would like to sync doctrine with the new postgres database with

php bin/console doctrine:migrations:sync-metadata-storage
php bin/console make:migration
php bin/console doctrine:migrations:migrate 

the generated migration has for each table something like this

        $this->addSql('ALTER TABLE post ALTER id TYPE INT');
        $this->addSql('ALTER TABLE post ALTER id DROP DEFAULT');
        $this->addSql('ALTER TABLE post ALTER id ADD GENERATED BY DEFAULT AS IDENTITY');
        $this->addSql('ALTER TABLE post ALTER user_id TYPE INT');
        $this->addSql('ALTER TABLE post ALTER "timestamp" TYPE TIMESTAMP(0) WITHOUT TIME ZONE');
        $this->addSql('ALTER TABLE post ALTER updated TYPE TIMESTAMP(0) WITHOUT TIME ZONE');
        $this->addSql('ALTER INDEX idx_16389_uniq_77e0ed58539b0606 RENAME TO UNIQ_77E0ED58539B0606');
        $this->addSql('ALTER INDEX idx_16389_idx_77e0ed58a76ed395 RENAME TO IDX_77E0ED58A76ED395');

after migration doctrine created TWO sequence files per table named: post_id_seq that has the correct sequence number let say 344 and post_id_seq1 that has a sequence of 1 which is NOT correct

here is after migration Screenshot 2024-07-24 at 08 06 54 Screenshot 2024-07-23 at 13 41 45

now if I insert a new record I get error

duplicate key value violates unique constraint "idx_16661_primary"

That is because Postgres is following post_id_seq1

So my question is what is going on ? I honestly do not understand what I am doing wrong and why doctrine seems to be creating two sequence files ?

signmeuptwice avatar Jul 23 '24 16:07 signmeuptwice