wmnnd

wmnnd

Ecto unique_constraint for primary key

Hi there,

I’m trying to capture constraint exceptions when a user attempts to create a database record with a primary key that already exists.
My Ecto struct has its primary key defined like this: @primary_key { :uuid, :binary_id, autogenerate: true }
I tried applying a unique_constraing in my changeset with unique_constraint(changeset, :uuid) but would still get an exception that said:

** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * unique: PRIMARY

If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:

    * unique: %TABLE_NAME_uuid_index

Only after I added name: :PRIMARY as an additional opts argument to unique_constraint/3, everything went fine: unique_constraint(changeset, :uuid, name: :PRIMARY)

Now why is it that I have to specify :PRIMARY as the name of the constraint? Where does that name come from?
I could not find anything about that in the Ecto docs.

Marked As Solved

josevalim

josevalim

Creator of Elixir

PRIMARY is the name of the constraint Postgres gives to primary keys. By default we inflect a name based on a unique index, so you need to be explicitly in the case of primary key matches.

Last Post!

wmnnd

wmnnd

Thanks for the explanation, that’s already what I suspected. I’m using MariaDB but it seems to use the same naming convention.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement