question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On conflict on constraint upsert + postgres

See original GitHub issue

Environment

Knex version: 0.95.1 Database + version: PostgreSQL 13.0, compiled by Visual C++ build 1914, 64-bit OS: Win 10

Feature discussion / request

https://knexjs.org/#Builder-onConflict

I haven’t found in the documentation how to upset rows with unique constraints.

Given following constraint:

ALTER TABLE public.assignments_mappings ADD CONSTRAINT assignments_mappings_task_id_user_id_unique UNIQUE (task_id, user_id)

I trying to perform upsert with following code:

 return await db("assignments_mappings")
      .insert(
        tasksID.map(id => ({ task_id: id, user_id, company_id: 1 }))
      )
      .onConflict('assignments_mappings_task_id_user_id_unique')
      .merge()
      .returning('id')

Which generates

error: insert into "assignments_mappings" ("company_id", "task_id", "user_id") values ($1, $2, $3), ($4, $5, $6), ($7, $8, $9), ($10, $11, $12) on conflict ("assignments_mappings_task_id_user_id_unique") do update set "company_id" = excluded."company_id", "task_id" = excluded."task_id", "user_id" = excluded."user_id" returning "id" - column "assignments_mappings_task_id_user_id_unique" does not exist

For successful operation, the output gotta be on conflict on constraint. How can I solve it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
sjchmielacommented, Oct 25, 2021

Ok, wrapping lower(email) with db.raw did the trick. Thanks for prompting me to investigate it further!

0reactions
abhi-nesteggcommented, Dec 8, 2021

Can anyone confirm that this is the way onConflict will behave in general?

For eg. I am going to use it for an index that is created like so:

CREATE UNIQUE INDEX accounts_unique_account_index ON my_accounts (COALESCE(company_id, user_id), account_id);

So, the corresponding onConflict clause, as per above discussion, will be:

.onConflict([knex.raw("COALESCE(company_id, user_id)"), 'account_id']).merge()

Just want to make sure that it’s a feature and not a side effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL Upsert Using INSERT ON CONFLICT statement
This tutorial shows you how to use the PostgreSQL upsert feature to insert or update data if the row that is being inserted...
Read more >
How to use `INSERT ON CONFLICT` to upsert data in ... - Prisma
PostgreSQL's INSERT...ON CONFLICT construct allows you to choose between two options when a proposed record conflicts with an existing record. Both DO NOTHING ......
Read more >
postgresql ON CONFLICT ON CONSTRAINT for 2 constraints
Specifies which conflicts ON CONFLICT takes the alternative action on by choosing arbiter indexes. Either performs unique index inference, or names a constraint...
Read more >
Use INSERT ON CONFLICT to overwrite data - AnalyticDB for ...
The INSERT ON CONFLICT statement allows you to update an existing row that ... This feature is also known as UPSERT or INSERT...
Read more >
PostgreSQL ON CONFLICT - eduCBA
PostgreSQL on conflict is used to insert the data in the same row twice, which the constraint or column in PostgreSQL identifies values....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found