ent icon indicating copy to clipboard operation
ent copied to clipboard

ON CONFLICT DO UPDATE requires inference specification or constraint name

Open Southclaws opened this issue 4 years ago • 4 comments

  • [x] The issue is present in the latest release.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

I think this is related to the ID field, it generates with two indexes: image

one named pkey and the other named id_key

Code that fails: https://github.com/Southclaws/storyden/blob/main/api/src/resources/category/db.go#L24-L45

Schema: https://github.com/Southclaws/storyden/blob/main/api/src/infra/db/schema/category.go

I followed the docs here for custom ID types: https://entgo.io/docs/schema-fields#id-field and I'm probably going to switch from UUID to something like CUID in future so I want this to work without using the auto increment IDs.

Expected Behavior 🤔

Updates/upserts to work fine with one single unique ID constraint.

Also, if there are multiple constraints, how would that be done?

Steps to Reproduce 🕹

Grab the Storyden code above and run the test for TestCreatePost which initially seeds the database with users and categories, the seed data for categories is what fails.

Your Environment 🌎

Tech Version
Go 1.18
Ent 0.10.1
Database Postgres
Driver github.com/jackc/pgx/v4/stdlib

Southclaws avatar Apr 23 '22 17:04 Southclaws

@Southclaws I think this is Postgres specific. It works when you specify the conflict target columns using .OnConflictColumns(...)

TheDivic avatar Jul 12 '22 16:07 TheDivic

I got stuck on the same error and found a document about upsert in PostgreSQL.

https://entgo.io/docs/crud/#upsert-one

Syuparn avatar Sep 26 '22 03:09 Syuparn

imo, this is a bug in postgresql.

  1. user should be able to specify an action "on any conflict" ... if another conflict occurs... throw an error
  2. user should be able to specify multiple on-conflict clauses one for each conflict type

neither of those are available, leaving postgres on-conflict-do-nothing the only way to go!

earonesty avatar Apr 10 '24 21:04 earonesty