Many to many creation of todolist and todo_items when todo_items are already created

I am referring to Polymorphic associations with many to many — Ecto v3.8.4
By using

def changeset(struct, params \\ %{}) do
    struct
    |> Ecto.Changeset.cast(params, [:name])
    |> Ecto.Changeset.cast_assoc(
      :todo_items,
      required: true
    )
  end

and

def create_todo_list(attrs \\ %{}) do
    %TodoList{}
    |> TodoList.changeset(attrs)
    |> Repo.insert()
  end

I believe Ecto will attempt to create TodoList, TodoItems, TodoListItems all at once. But what i wish to achieve is to create TodoList but only update TodoListItems since for me TodoItems are already created beforehand. Tried changing cast_assoc to put_assoc but it doesn’t quite work. How can i do this?

Its ok i think i got what i want here: Constraints and Upserts — Ecto v3.8.4

1 Like

One major premise of introducing the many_to_many association in this section was specifically the complexity involved in creating TodoItems along with their join records.

Since you don’t want that, write what you mean: you want to create todo_list_items along with a todo_list so call cast_assoc on todo_list_items in TodoList.changeset.