SeaQL / SeaQL/sea-orm

Prepared statement errors `there is no unique or exclusion constraint matching the ON CONFLICT specification` from database but unprepared statement works

Open
#2,495 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:query-builder Postgres
Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Description

When running a query using on conflict update clauses, it errored from the database on execution:
2025-02-15T00:32:52.275908Z ERROR run: task::review: error=Execution Error: error returned from database: there is no unique or exclusion constraint matching the ON CONFLICT specification.
However, manually testing the interpolated query in TablePlus, the query executed successfully,
So, I tried building the query into a string first and then executing an unprepared statement in the transaction and it works fine.
The canonical path of executing the prepared statement ought to work though.

Steps to Reproduce

  1. Migrate up the schema like so, taking liberties to ensure it builds.
    Table::create()
      .table(crate::ReviewComment::Table)
      .if_not_exists()
      .col(pk_auto(crate::ReviewComment::CommentId))
      .col(integer(crate::ReviewComment::ReviewId))
      .foreign_key(
        ForeignKey::create()
          .name("review_comment_review_fkey")
          .from(crate::ReviewComment::Table, crate::ReviewComment::ReviewId)
          .to(crate::Review::Table, crate::Review::ReviewId)
          .on_delete(ForeignKeyAction::Cascade)
          .on_update(ForeignKeyAction::Cascade),
      )
      .col(integer(crate::ReviewComment::AccountId))
      .foreign_key(
        ForeignKey::create()
          .name("review_comment_account_fkey")
          .from(crate::ReviewComment::Table, crate::ReviewComment::AccountId)
          .to(crate::Account::Table, crate::Account::AccountId)
          .on_delete(ForeignKeyAction::Cascade)
          .on_update(ForeignKeyAction::Cascade),
      )
      .col(enumeration(
        crate::ReviewComment::ReviewStage,
        crate::ReviewStage::ReviewStage,
        crate::ReviewStage::iter().skip(1),
      ))
      .col(enumeration(
        crate::ReviewComment::CommentKind,
        crate::ReviewCommentKind::ReviewCommentKind,
        crate::ReviewCommentKind::iter().skip(1),
      ))
      .col(enumeration(
        crate::ReviewComment::CommentStatus,
        crate::ReviewCommentStatus::ReviewCommentStatus,
        crate::ReviewCommentStatus::iter().skip(1),
      ))
      .col(
        array(
          crate::ReviewComment::InferenceReasoning,
          ColumnType::string(None),
        )
        .default(Expr::val("{}")),
      )
      .col(
        array(
          crate::ReviewComment::InferenceSnippets,
          ColumnType::string(None),
        )
        .default(Expr::val("{}")),
      )
      .col(string(crate::ReviewComment::Content))
      .col(string_null(crate::ReviewComment::Preview))
      .col(integer_null(crate::ReviewComment::CriteriaId))
      .foreign_key(
        ForeignKey::create()
          .name("review_comment_criteria_fkey")
          .from(
            crate::ReviewComment::Table,
            crate::ReviewComment::CriteriaId,
          )
          .to(crate::Criteria::Table, crate::Criteria::CriteriaId)
          .on_delete(ForeignKeyAction::Cascade)
          .on_update(ForeignKeyAction::Cascade),
      )
      .col(enumeration_null(
        crate::ReviewComment::InferenceDecision,
        crate::InferenceDecision::InferenceDecision,
        crate::InferenceDecision::iter().skip(1),
      ))
      .col(
        timestamp_with_time_zone(crate::ReviewComment::CreatedAt)
          .default(Expr::current_timestamp()),
      )
      .col(
        timestamp_with_time_zone(crate::ReviewComment::UpdatedAt)
          .default(Expr::current_timestamp()),
      )
      .pipe(std::mem::take)
      .pipe(|table| manager.create_table(table))
      .await?;
    Index::create()
      .if_not_exists()
      .name("review_comment_review_id_review_stage_key")
      .table(crate::ReviewComment::Table)
      .col(crate::ReviewComment::ReviewId)
      .col(crate::ReviewComment::ReviewStage)
      .pipe(std::mem::take)
      .pipe(|index| manager.create_index(index))
      .await?;
    Index::create()
      .if_not_exists()
      .name("review_comment_review_id_review_stage_criteria_id_for_comment_kind_kritiq_unique_key")
      .table(crate::ReviewComment::Table)
      .col(crate::ReviewComment::ReviewId)
      .col(crate::ReviewComment::ReviewStage)
      .col(crate::ReviewComment::CriteriaId)
      .unique()
      .cond_where(
        Expr::col(crate::ReviewComment::CommentKind).is_in::<Value, _>([
          entity::sea_orm_active_enums::ReviewCommentKind::KritiqScreening.into(),
          entity::sea_orm_active_enums::ReviewCommentKind::KritiqExtraction.into(),
        ]),
      )
      .pipe(std::mem::take)
      .pipe(|index| manager.create_index(index))
      .await?;
  1. Generate the entities via sea-orm-cli.
  2. Insert a record like so. Uncomment the code to reproduce the error.
    ReviewComment::insert_many(comments)
      .on_conflict(
        sea_orm::sea_query::OnConflict::columns([
          entity::review_comment::Column::ReviewId,
          entity::review_comment::Column::ReviewStage,
          entity::review_comment::Column::CriteriaId,
        ])
        .target_and_where(
          Expr::col(entity::review_comment::Column::CommentKind).is_in([
            entity::sea_orm_active_enums::ReviewCommentKind::KritiqScreening.as_enum(),
            entity::sea_orm_active_enums::ReviewCommentKind::KritiqExtraction.as_enum(),
          ]),
        )
        .update_columns([
          entity::review_comment::Column::AccountId,
          entity::review_comment::Column::CommentStatus,
          entity::review_comment::Column::InferenceReasoning,
          entity::review_comment::Column::InferenceSnippets,
          entity::review_comment::Column::Content,
          entity::review_comment::Column::Preview,
          entity::review_comment::Column::InferenceDecision,
          entity::review_comment::Column::UpdatedAt,
        ])
        .to_owned(),
      )
      // HACK: Using the canonical execution method, we get the error:
      // 2025-02-15T00:32:52.275908Z ERROR run: task::review: error=Execution Error: error returned from database: there is no unique or exclusion constraint matching the ON CONFLICT specification
      // So avoid it by building the query first and executing an unprepared statement.
      // .exec_without_returning(transaction)
      // .await?;
      .build(sea_orm::DbBackend::Postgres)
      .to_string()
      .pipe_deref(|sql| transaction.execute_unprepared(sql))
      .await?;
Expected Behavior

Executing via exec_without_returning should work.

Actual Behavior

Have to first build the query into string, and then execute an unprepared statement for it to work.

Reproduces How Often

Always reproducible.

Workarounds

Have to first build the query into string, and then execute an unprepared statement for it to work.

Reproducible Example

See above for steps to reproduce.

Versions

NeonDB Postgres 17

sea-orm = { version = "1.1.4", features = [
    "debug-print",
    "macros",
    "runtime-tokio-rustls",
    "sqlx-postgres",
] }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided migration and insert example, then compare SeaORM's exec_without_returning path with the build(...).to_string() and execute_unprepared workaround on PostgreSQL. Trace OnConflict::target_and_where and the prepared execution entry point; done means the canonical prepared execution succeeds with the partial unique index and the regression is covered by a reproducible test.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgres, rust
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.