silverdr
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?
Trending in Questions
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
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
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Can you show the code for
Item.changeset? I’ve got several questions:Itemchangesets getting any value forid?Ecto.Repo; is there anything unusual inCart.changeset?silverdr
There’s no explicit assignment. DB sequence assigns it upon creating record. Here the record is not created and the ‘id’ is not in the changeset. OTOH the form does have an
idfieldbut values are empty there and as discussed in another thread there should be no need for “scrubbing” them. I might try doing this still manually and see if that helps
I take Ecto adds it. No explicit addition from the application’s code
I think Ecto adds it before DB has any chance of having a stab at checking. It looks to me like it sees the two (empty)
ids and concludes they’re the same before going further: a) the parent record is not inserted, b) there’s no DB activity in the logs, and to answer the question there’s nothing uncommon in theCart.changeset. Casting attrs, and casting association:plus some validations but only on the cart data itself
Sure!
silverdr
Yes, removing
ids from form data helps. Hmm…dodo
Just ran into this problem as well, shouldn’t
castremove any non-permitted params?al2o3cr
This error isn’t coming from the database, it’s from the internals of
cast_assoc:https://github.com/elixir-ecto/ecto/blob/3ee206b63c13272f0d7a5d48c37dbc1045f07ea0/lib/ecto/changeset/relation.ex#L386-L394
Two thoughts on what could be causing this:
idparams casting tonilif it’s empty. Integers definitely do that, but (for instance) plain strings would not. What type are your ID columns?action, but depend on specific values like:insertfor some behaviors. The original post was setting the action to:validate; does the same issue persist if the action is set to:insert?silverdr
At least in my case I still believe that
Ectodoes this (adds the error) before anything goes to the DB level. And before doing casts/whatever. Kinda soft-bug it seemsdodo
Hi, thank you for the quick reply!
I am using regular auto-incrementing integer ids (bigserial). I set the id of newly created items to
-1in the frontend to avoid nullable types, I just ended up not sending those in the request to fix the issue.The
actionfield is not set manually, I am usingcast_assocwith ahas_manyrelation andon_replace: :deletebecause the frontend always sends all associations. The error appeared when the action was:updateon the root entity and:inserton the associated (has_many) entities, those were preloaded but empty before updating.I understand that Ecto relies on the
idfor finding existing entities and proceed depending on theon_replaceoption, but I thought theidwould just be ignored when it does not match any existing entity.Now that I think about it I actually prefer the existing behavior, it seems safer than just ignoring invalid ids
.