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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews