goqu icon indicating copy to clipboard operation
goqu copied to clipboard

Upsert constraint violations

Open antooa opened this issue 4 years ago • 6 comments

I'm always frustrated when I need to do UPSERT in PostgreSQL. It all works well when I need to do something like in examples. Unfortunately, I didn't find any way to use additional clauses, e.g. I didn't find any way to use ...ON CONFLICT .. ON CONSTRAINT... Also, DoUpdate(target string, update interface{}) supports only one target field. How to use It if I need something like this: ON CONFLICT (a, b) .... ?

Describe the solution you'd like I would like to have a solution which I can use to handle constraint conflicts while UPSERT

Dialect

  • postgres

antooa avatar Jul 28 '20 08:07 antooa

Any movement here? I noticed that DoNothing() does not support an action. So you cannot express INSERT INTO ... ON CONFLICT my_unique_constraint DO NOTHING.

atombender avatar Jul 05 '21 11:07 atombender

Hi I have the same question. I want to do "on conflict (field1, field2) do nothing or update....

Is there a way to do this now?

timuckun avatar Aug 20 '21 04:08 timuckun

I'm looking to do add to my query

ON CONFLICT ON CONSTRAINT unique_constraint DO UPDATE SET count = p.count + EXCLUDED.count

But I'm struggling to figure out how to do this. Is this currently not supported by goqu?

aranw avatar Oct 14 '21 17:10 aranw

Just to summarize this very old issue, there's no way to express the target in INSERT ... ON CONFLICT (target) DO NOTHING, since DoNothing() does not support a target name.

@doug-martin The logical solution seems to be do make doNothingConflict support a target, just like conflictUpdate, and update onConflictSQL() to handle it.

atombender avatar Jan 04 '22 23:01 atombender

I stumbled on this issue. Here's the expression I want to build for PostgreSQL:

... ON CONFLICT (user_id, method, network, identifier) WHERE deletion_ts IS NULL DO NOTHING

exavolt avatar Jul 03 '22 11:07 exavolt

Here is my escape:

dialect.Insert("some_table").Cols("col1", "col2", "col3").Vals(vals...).
		OnConflict(goqu.DoUpdate(
			"ON CONSTRAINT smth_unique",
			goqu.C("col1").Set(goqu.L("EXCLUDED.col1")),
		))

tandem97 avatar Nov 27 '23 10:11 tandem97