On conflict on constraint upsert + postgres
See original GitHub issueEnvironment
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:
- Created 3 years ago
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, wrapping
lower(email)
withdb.raw
did the trick. Thanks for prompting me to investigate it further!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:
So, the corresponding onConflict clause, as per above discussion, will be:
Just want to make sure that it’s a feature and not a side effect.