Update all association

I am trying to update the deleted_at column in assoc which is has_many, but i am getting error

(Postgrex.Error) ERROR 23502 (not_null_violation) null value in column “uuid” violates not-null constraint

i think its trying to create an assoc but i want to update all assoc

def get_assoc(uuid) do
    cr =
      Response 
      |> Repo.get_by(uuid: uuid) 
      |> Repo.preload(:favorites)

     cr 
      |> Ecto.Changeset.change()
      |> Ecto.Changeset.put_assoc(:favorites, [%Favorite{deleted_at: DateTime.utc_now()} | cr.favorites])
      |> Repo.update!()
    {:ok, cr}
  end

You can’t delete something that is supposed to be not_null.

Can you share the migration where you have uuid not_null option set?

Also what kind of associations do you have many_to_many has_many for favorite?

1 Like