2

has_one アソシエーションの更新で Ecto がエラーを出しています:

問題のモデル:

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

パラメータ

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.

ドキュメントに基づいて、親モデルのチェンジセット関数が親から孤立した「モノ」を見ているように見えますが、理由がわかりませんか?

新規/作成アクションは問題なく機能します。

4

2 に答える 2

1

これは Ecto のバグでした。このコミットでマスターで修正されました。

https://github.com/elixir-lang/ecto/commit/6c1c04304ebecf5ccae4a49008deab814e034d2b

于 2015-10-16T00:56:33.237 に答える