enkr1
How to handle unique constraint when using put_assoc
Hi community,
I am doing a test case and I have an associative table with FKs user_id and role_id. When I tried to insert with the repeated FKs (1, 1) like:
%User
|> User.creation_changeset(%{
email: "unqiue@gmail.com",
password: "superstrongpassword",
role_ids: [1, 1] # I have some pre-process to convert ID list to object
})
|> Repo.insert()
My user_role changeset:
def changeset(user_role, attrs) do
user_role
|> cast(attrs, [:user_id, :role_id])
|> validate_required([])
|> unique_constraint([:role_id, :user_id], name: :user_role_items_user_id_role_id_index)
end
And this is the error i get:
test/perfreview/accounts_test.exs:586
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* user_role_items_user_id_role_id_index (unique_constraint)
If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.
The changeset defined the following constraints:
* users_email_index (unique_constraint)
# ...
I have tested the changeset, it is working fine. Just that the changeset cannot be hit in put_assoc/3.
Thank you so much in advance.
Best wishes,
Jing Hui PANG.
First Post!
alexsysm
are you sure that you made a unique index before you query?
quote “”"
The changeset defined the following constraints:
* users_email_index (unique_constraint)
“”"
I am guessing that you just created email index only.
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217
Let’s say I have a map with required and optional k...
New
Other popular topics
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







