benonymus
Warning found duplicate primary keys for association/embed in liveview with nested cast_assoc changeset
Hey,
I have a liveview application with a nested changeset.
There is study on top.
This has a cast_assoc for scenarios.
And scenarios has a cast_assoc for scenario_tasks.
I am having a problem on the scenario tasks level.
When I add a 3rd new scenario_task in liveview I am seeing this warning:
[warning] found duplicate primary keys for association/embed
:scenario_tasksinScenario. In case of duplicate IDs, only the last entry with the same ID will be kept. Make sure that all entries in:scenario_taskshave an ID and the IDs are unique between them
I think this is because now in cast there are multiple scenario_tasks with nil for id.
This seems to not cause any problems for add/update, but when I am deleting these entries either all of them go at once, or the last 2 go at once.
I am also not sure if the interaction with the changeset is correct (legacy code).
Add is done like this:
def handle_add_task(socket, scenario_i) do
{scenario_index, _rem} = Integer.parse(scenario_i)
study_changeset = socket.assigns.study_changeset
scenarios = Ecto.Changeset.get_field(study_changeset, :scenarios, []) || []
nth_scenario = Enum.at(scenarios, scenario_index)
updated_tasks = nth_scenario.scenario_tasks ++ [%{title: ""}]
nth_scenario_with_new_task =
Ecto.Changeset.change(nth_scenario, %{scenario_tasks: updated_tasks})
scenarios_with_i_updated =
List.replace_at(scenarios, scenario_index, nth_scenario_with_new_task)
updated_study_changeset =
Ecto.Changeset.put_change(study_changeset, :scenarios, scenarios_with_i_updated)
{:noreply, assign(socket, study_changeset: updated_study_changeset)}
end
Remove is done like this:
def handle_event("remove_task", %{"task" => task_i, "scenario" => scenario_i}, socket) do
{scenario_index, _rem} = Integer.parse(scenario_i)
{task_index, _rem} = Integer.parse(task_i)
study_changeset = socket.assigns.study_changeset
scenarios = Ecto.Changeset.get_field(study_changeset, :scenarios, []) || []
nth_scenario = Enum.at(scenarios, scenario_index)
scenario_tasks = Ecto.Changeset.get_field(nth_scenario, :scenario_tasks, [])
updated_tasks = List.delete_at(scenario_tasks, task_index)
nth_scenario_changeset = Ecto.Changeset.change(nth_scenario, %{scenario_tasks: updated_tasks})
updated_scenarios = List.replace_at(scenarios, scenario_index, nth_scenario_changeset)
study_changeset = Ecto.Changeset.put_change(study_changeset, :scenarios, updated_scenarios)
{:noreply, assign(socket, study_changeset: study_changeset)}
end
My questions would be, is the handling of the changes to the changeset fine this way?
Any ideas how to handle the duplicate primary keys warning?
Thank you
Marked As Solved
benonymus
More or less got it sorted, this helped: Warning about duplicated primary keys for new records · Issue #3514 · elixir-ecto/ecto · GitHub
I am changed ti code, to not pass tasks as structs, but always as maps.
Also Liked
al2o3cr
I’ve encountered this before when doing “add another” kinds of patterns, but I haven’t written the blog post I’ve been promising myself I’d write yet.
The short short version: put_change does not like seeing action: :replace changesets in its input for IDs it already has one for, so filter them out.
This replicates the behavior of changesets in old-school server-side rendering: phoenix_ecto skips them altogether when generating inputs:
Popular in Questions
Other popular topics
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
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









