Skip to content

Loading fixtures, does not update sequence (Postgres) #8298

@tino415

Description

@tino415

When I load fixtures, sequence number of tables are not updated or updated to wrong values,
I'm using this fix:

<?php

namespace tests\codeception\fixtures;

use yii\test\ActiveFixture;
class DocumentTypeFixture extends ActiveFixture {

    public $modelClass = 'app\models\DocumentType';

    public function load() {
        parent::load();

        if($this->db->drivername === 'pgsql') {
            $this->db->createCommand("SELECT pg_catalog.setval(".
                "pg_get_serial_sequence('document_type', 'id'),".
                "(SELECT MAX(id)+1 FROM document_type)".
            ")")->execute();
        }
    }
}

Error message:

1) tests\codeception\unit\crawlers\SuperMusicDocumentCrawlerTest::testCrawling
yii\db\IntegrityException: SQLSTATE[23505]: 
Unique violation: 7 ERROR:  duplicate key value violates unique constraint "document_type_pkey"
DETAIL:  Key (id)=(3) already exists.
The SQL being executed was: INSERT INTO "document_type" ("name") VALUES ('taby')

Fixture data:

<?php

return [
    'type' => [
        'id' => 1,
        'name' => 'type',
    ],
    'type' => [
        'id' => 2,
        'name' => 'type2',
    ],
    'type' => [
        'id' => 3,
        'name' => 'type333',
    ],
];

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions