外生是给我上更新HAS_ONE关联的错误:Ecto.Changeset相关模型误差
模型的问题:
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
PARAMS
parent =%{“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.
基于它看起来像父模式变更功能是在看到‘东西’从父成为孤儿的文档 - 但我不能明白为什么?
新建/创建操作工作得很好。
如果我想更改传播会怎么做:delete删除以前的事情,用新的东西代替它? –
@RichardHolland有几个选项 - 我在答案中添加了更多详细文档的链接。 – Gazler
谢谢。 :删除得到我的愿望 –