Ecto 在更新 has_one 关联时给我一个错误:
有问题的型号:
defmodule Something.Parent do
has_one :thing, Something.thing
@required_fields ~w(field1 field2 thing)
end
控制器更新动作
def update(conn, %{"id" => id, "parent" => parent_params}) do
parent = Repo.get!(Parent, id) |> Repo.preload(:thing)
changeset = Parent.changeset(parent, parent_params)
case Repo.update(changeset) do
{:ok, _parent} ->
conn
|> put_flash(:info, "Parent updated successfully")
|> redirect(to: parent_path(conn, :index))
{:error, changeset} ->
render(conn, "edit.html", parent: parent, changeset: changeset)
end
end
参数
父 = %{"some_number" => "902", "thing" => %{"somethingfield" => "blah", "parent_id" => "8"}
错误
you are attempting to change relation :thing of
Whatever.Parent, but there is missing data.
By default, if the parent model contains N children, at least the same
N children must be given on update. In other words, it is not possible
to orphan embed nor associated records, attempting to do so results
in this error message.
It is possible to change this behaviour by setting :on_replace when
defining the relation. See `Ecto.Changeset`'s section on related models
for more info.
根据文档,父模型上的变更集函数看起来像是从父模型中分离出来的“事物”——但我不明白为什么?
新/创建操作工作得很好。