silverdr
Ecto strange (?) one-to-many validation errors on children
I have a one-to-many relation where I – say – add items to cart. Once I add one item it’s fine. When I add another I get:
errors: [id: {"has already been taken", []}]
on the second item, even if it’s not persisted. The whole changeset looks (simplified) like:
#Ecto.Changeset<
action: :validate,
changes: %{
[...]
items: [
#Ecto.Changeset<
action: :insert,
changes: %{
description: "gin",
[...]
},
errors: [],
data: #MyApp.Cart.Item<>,
valid?: true
>,
#Ecto.Changeset<
action: :insert,
changes: %{
description: "tonic",
[...]
},
errors: [id: {"has already been taken", []}],
data: #MyApp.Cart.Item<>,
valid?: false
>
],
},
errors: [],
[...]
valid?: false
>
I “put” the action “validate” on the parent in the changeset like:
changeset = %Cart{}
|> Cart.changeset(add_static_fields(params, socket))
|> Map.put(:action, :validate)
but apparently children have action “insert” in their respective changesets. Since I am not “inserting” the parent anyway, why do the children complain about their "id being taken"? No id's been assigned yet, or what part of my brain is not working today?
First Post!
al2o3cr
Can you show the code for Item.changeset? I’ve got several questions:
- where are the
Itemchangesets getting any value forid? - who is adding that error?
- the DB-side uniqueness validations would need to somehow get ahold of an
Ecto.Repo; is there anything unusual inCart.changeset?
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
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
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
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New







