pankaj-ag
Issues with multiple conflict targets in Ecto Upsert
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?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security











First 9 of 9 Posts!
thiagomajesk
Shouldn’t it be:
Instead of:
?
Also, check if you have a constraint in your migration like:
ehayun
look here
There is a section with Complex Constraints
pankaj-ag
Yes, there was a typo sorry
It’s not a combined key. I have updated my question please have a look?
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
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
What is your use-case?
pankaj-ag
@thiagomajesk I want to do the different operations on different unique key conflicts.
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:
You can also check constraints manually with
unique_constraint/3and 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_targetoption. It seems that you can use fragments:axelson
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.