Skip to content

Upsert doesn't work when there's unique index #1142

@samdark

Description

@samdark

Description

I've got the bug using PostgreSQL.

Create a table

$b->createTable('page', [
    'id' => $cb::uuidPrimaryKey(),
    'title' => $cb::string()->notNull(),
    'slug' => $cb::string()->notNull()->unique(),
    'text' => $cb::text()->notNull(),
    'created_at' => $cb::dateTime(),
    'updated_at' => $cb::dateTime(),
]);

Do upsert:

$this->connection->createCommand()->upsert('{{%page}}', [
    'id' => $page->id,
    'title' => $page->title,
    'slug' => $page->getSlug(),
    'text' => $page->text,
    'created_at' => $page->createdAt,
    'updated_at' => $page->updatedAt,
])->execute();

Get the error:

QLSTATE[42P10]: Invalid column reference: 7 ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
The SQL being executed was: INSERT INTO "page" ("id", "title", "slug", "text", "created_at", "updated_at") VALUES ('019b5c77-1c8b-72bd-a0c9-547987f9cf9c', 'Test', 'test', 'test', '2025-12-26 21:01:08', '2025-12-26 21:01:08') ON CONFLICT ("id", "slug") DO UPDATE SET "title"=EXCLUDED."title", "text"=EXCLUDED."text", "created_at"=EXCLUDED."created_at", "updated_at"=EXCLUDED."updated_at"

Package version

2.0.0

PHP version

8.2

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