具有has_many关系的Phoenix模型不会更新w / o预加载关系

时间:2017-02-09 05:21:01

标签: elixir phoenix-framework ecto

我正在开发凤凰应用程序,这是Evernote的个人版本。我有一个Book模型,has_many Note条记录。

这是我的书模型:

defmodule Notebook.Book do
  use Notebook.Web, :model

  schema "books" do
    field :name, :string, default: ""
    has_many :notes, Notebook.Note
    belongs_to :user, Notebook.User
    timestamps()
  end

  @doc """
    Book changeset. Name field required.
  """
  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:name])
    |> validate_required(:name)
  end
end

我的控制器中有一个更新端点:

def update(conn, %{"id" => id, "book" => book_params}) do
  existing_book = Repo.get(Book, id)
  changeset = Book.changeset(existing_book, book_params)

  case Repo.insert(changeset) do
    {:ok, book} ->
      conn
      |> put_status(:ok)
      |> render("show.json", book: book)

    {:error, changeset} ->
      conn
      |> put_status(:unprocessable_entity)
      |> render("error.json", message: changeset.errors)
  end
end

测试:

test "with a valid jwt", %{conn: conn, jwt: jwt} do
  book = insert(:book)
  resp = conn
  |> put_req_header("authorization", "Bearer: #{jwt}")
  |> put(book_path(Endpoint, :update, book, book: %{name: "New  Book"}))
  |> json_response(:ok)

  assert resp["data"]["book"]["name"] == "New Book"
end

当我运行测试时,我收到此错误:

** (RuntimeError) attempting to cast or change association `notes`   from `Notebook.Book` that was not loaded. Please preload your associations before manipulating them through changesets

我发送的参数仅为name。看一下这个,我发现了一个related issue,但由于我没有使用cast_assoc,我觉得它不适用。

我无法弄清楚我在这里做错了什么。我理解(我认为)关于在Ecto中预加载关系,但在这种情况下,我不是更新依赖的Note记录,只是Book记录的一个字段,所以我不需要预加载。

整个回购邮件是here

1 个答案:

答案 0 :(得分:3)

您似乎在Repo.insert的{​​{1}}方法中使用了update。将其更改为BookController应修复它。