I'm trying to write a single changeset that will update a model and insert an association. I cannot find examples on how to use put_assoc/4
order = order
|> Proj.Order.changeset(%{state: "error", error_count: order.error_count + 1})
|> Ecto.Changeset.put_assoc(
:order_errors,
[Proj.OrderError.changeset(%Proj.OrderError{}, %{reason: "not_found"})])
|> Proj.Repo.update!
This prints out the following error:
** (Ecto.InvalidChangesetError) could not perform update because changeset is invalid.
* Changeset changes
%{order_errors: [%Ecto.Changeset{action: :insert, changes: %{id: nil, inserted_at: nil, order_id: nil, reason: "not_found", updated_at: nil}, constraints: [], errors: [order_id: "can't be blank"], filters: %{}, model: %Proj.OrderError{__meta__: #Ecto.Schema.Metadata<:built>, id: nil, inserted_at: nil, order: #Ecto.Association.NotLoaded<association :order is not loaded>, order_id: nil, reason: nil, updated_at: nil}, optional: [], opts: [], params: %{"reason" => "not_found"}, prepare: [], repo: nil, required: [:order_id, :reason], types: %{id: :id, inserted_at: Ecto.DateTime, order_id: :id, reason: :string, updated_at: Ecto.DateTime}, valid?: false, validations: []}], state: "error"}
* Changeset params
%{"error_count" => 1, "state" => "error"}
* Changeset errors
[]
Any examples I can look at for put_assoc/4
? How can I find why the changeset is invalid?
The goal of doing things this way is that I hope the new order
would have order_errors
preloaded.