benonymus

benonymus

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_tasks in Scenario. In case of duplicate IDs, only the last entry with the same ID will be kept. Make sure that all entries in :scenario_tasks have 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

Showing Posts 1 to 2

benonymus

benonymus OP

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.

al2o3cr

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:

https://github.com/phoenixframework/phoenix_ecto/blob/650aa8b32d4ba9ed8bb193c2fd03708629032b8e/lib/phoenix_ecto/html.ex#L168-L173

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews