pankaj-ag

pankaj-ag

I have a table called languages with 4 fields, label, description, locale, and is_active.

label and locale are two different unique fields (not composite) in this table. I am trying to write an upsert function in a way that if the conflict happens on any unique filed (label or locale) then it should just update the label field and give the results.
This is my implementation

Migration file

  create unique_index(:languages, :label)
  create unique_index(:languages, :locale)

Context file

def upsert(attrs) do
    Repo.insert!(
      change_language(%Language{}, attrs),
      on_conflict: [set: [label: attrs.label]],
      conflict_target: [ :label, :locale]
    )
  end

but it’s giving me PostgreSQL error for the second element in the list

ERROR 42P10 (invalid_column_reference) there is no unique or exclusion constraint matching the ON CONFLICT specification
    (ecto_sql 3.4.4) lib/ecto/adapters/sql.ex:593: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto 3.4.4) lib/ecto/repo/schema.ex:661: Ecto.Repo.Schema.apply/4
    (ecto 3.4.4) lib/ecto/repo/schema.ex:263: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.4.4) lib/ecto/repo/schema.ex:164: Ecto.Repo.Schema.insert!/4

Can you please help me to figure out what is going on here?

I think it’s assuming that it’s a combined key. Is there any way to set two different unique keys as a conflict target?

Showing Posts 1 to 9

thiagomajesk

thiagomajesk

Shouldn’t it be:

conflict_target: [:label, :locale]

Instead of:

conflict_target: [:locale, :locale]

?

Also, check if you have a constraint in your migration like:

create unique_index(:languages, [:label, :local])
ehayun

ehayun

look here
There is a section with Complex Constraints

pankaj-ag

pankaj-ag OP

Yes, there was a typo sorry :man_facepalming:

It’s not a combined key. I have updated my question please have a look?

thiagomajesk

thiagomajesk

The error says you need a unique constraint in those columns, do you have those constraints in your migration?

Besides that, I’m not sure what could be wrong here. Maybe you should take a look at the documentation that talks about this, it’s pretty good actually: Constraints and Upserts — Ecto v3.14.0.

jpjvs

jpjvs

Has anyone figured out how to do what the OP has asked? I find myself in the same situation. When trying to pass multiple columns as conflict_targets for an Upsert Ecto interprets them as a composite key, not as separate unique keys which are all unique constraints individually.

thiagomajesk

thiagomajesk

What is your use-case?

pankaj-ag

pankaj-ag OP

@thiagomajesk I want to do the different operations on different unique key conflicts.

thiagomajesk

thiagomajesk

@pankaj-ag Could you be more specific than that?

I don’t think Ecto supports passing multiple columns (with different constraints) to an upsert. I could be wrong, but I think you have to specify one or the other.

Given that you have created your constraints in one of the following ways, you’d have to choose which one fits the upsert operation:

create unique_index(:languages, [:label, :local])
create unique_index(:languages, [:label])
create unique_index(:languages, [:local])

You can also check constraints manually with unique_constraint/3 and wrap it in a transaction. But again, I don’t think Ecto has a mechanism to check constraints in the way you want to. Perhaps you could check if this is a valid upsert operation in Postgres first since Ecto is just a thin layer of abstraction.

Also, check the documentation on the conflict_target option. It seems that you can use fragments:

  • :on_conflict - It may be one of :raise (the default), :nothing, :replace_all, {:replace_all_except, fields}, {:replace, fields}, a keyword list of update instructions or an Ecto.Query query for updates. See the “Upserts” section for more information.
axelson

axelson

Scenic Core Team

Since this post appears very popular (the link in post 2 has almost 500 views!) and I just hit this scenario I wanted to give an update on this thread.

From my (admittedly limited) research it appears that Postgres does not support setting a conflict target on two independent unique columns:

So you need to choose one of them at a time to choose as the upsert target, probably by first fetching the record based on both columns.

Either that or switch your requirements to instead create a compound/combined unique index if you deice to instead check only for uniqueness across both columns.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews