Put_assoc Cannot Perform Update to Many-to-Many

So I have a table of ItemA which has a many-to-many mapping to ItemB.

In the changeset of ItemA, the ItemB association gets preloaded and, then, the relevant ItemBs from the database are added via put_assoc().

When handling a list of ItemB that’s already associated with ItemA, the rows linking them are DELETEd from the mapping table (this makes sense as on_replace: is set to :delete).

However, they are, then, never INSERTed again, leaving my put_assoc() to not associate the struct.s I’ve provided with ItemA but to actively destroy the mappings.

Given that the association has no changes, I would expect (as the documentation says) for this to be skipped.

I didn’t think I was doing anything particularly complicated; the ItemA schema looks like

defmodule ItemA do
  use    Ecto.Schema;
  import Ecto.Changeset;

  schema "item_a" do

    many_to_many(:item_bs,                         ItemB,
                 join_through: "item_a_to_item_b", on_replace: :delete);


  def changeset(%ItemA{} = a, attrs) do
    a |> cast(attrs, […])    # these are actual struct.s from the database, in the code
      |> put_assoc(:item_bs, [%ItemB{}, %ItemB{}, %ItemB{}, %ItemB{}])

Yet, after a successful first INSERT, any subsequent UPDATEs DELETEs all entries from the mapping table while the next UPDATE INSERTs them into the mapping table while the next UPDATE DELETEs them while the next UPDATE

I just want it to get the database to match the modules/structs I give it, not do the literal opposite.