Ecto - 表条目未更新

时间:2017-07-15 06:58:09

标签: elixir ecto elixir-framework

以下代码更新了Product中的changeset。我还尝试使用新的"价格"更新ProductShop。在changeset2,但它没有得到更新。我检查了所有重要部分,price有一个值,product_shop有一个值,并且"Price updated"被打印到控制台。

  put "/products" do
    errors = {}
    IO.inspect(conn.body_params)

    product = Api.Product |> Api.Repo.get(conn.query_params["p_id"])
    shop = Api.Shop |> Api.Repo.get(conn.query_params["s_id"])

    params = for key <- ~w(image description), value = conn.body_params[key], into: %{}, do: {key, value}
    changeset = Api.Product.changeset(product, params)
    case Api.Repo.update(changeset) do
      {:ok, product} -> 
        errors = Tuple.append(errors, "Product updated")
      {:error, changeset} -> 
        errors = Tuple.append(errors, "Product not updated")
    end

    pid = conn.query_params["p_id"]
    sid = conn.query_params["s_id"]
    price = Float.parse(conn.body_params["price"])
    price1 = elem(price, 0)
    IO.inspect(price1)

    product_shop = Api.Repo.get_by(ProductShop, s_id: sid, p_id: pid)
    IO.inspect(product_shop)

    changeset2 = Api.ProductShop.changeset(product_shop, %{price: price1})
    case Api.Repo.update(changeset2) do
      {:ok, product_shop} -> 
        errors = Tuple.append(errors, "Price updated")
      {:error, changeset2} -> 
        errors = Tuple.append(errors, "Price not updated")
    end

    IO.inspect(errors)

    conn
      |> put_resp_content_type("application/json")
      |> send_resp(200, Poison.encode!(%{
          successs: "success",
          errors: Tuple.to_list(errors)
      }))
  end

为什么ProductShop在填充price时不会更新,product_shop也是如此?

ProductShop.ex

defmodule Api.ProductShop do
  use Ecto.Schema
  import Ecto.Changeset
  import Api.Repo
  import Ecto.Query

  @derive {Poison.Encoder, only: [:s_id, :p_id]}
  schema "product_shops" do
    field :s_id, :integer
    field :p_id, :integer
    field :not_in_shop_count, :integer
    field :price, :float
  end

  def changeset(product_shop, params \\ %{}) do
    product_shop
    |> cast(params, [:s_id, :p_id])
    |> validate_required([:s_id, :p_id])
    |> unique_constraint(:s_id, name: :unique_product_shop)
  end

  def insert_product_shop(conn, product_id, shop_id, price) do
    changeset = Api.ProductShop.changeset(%Api.ProductShop{p_id: product_id, s_id: shop_id, not_in_shop_count: 0, price: price})
    errors = changeset.errors
    valid = changeset.valid?
    case insert(changeset) do
      {:ok, product_shop} ->
        {:ok, product_shop}
      {:error, changeset} ->
        {:error, :failure}
    end
  end

  def delete_all_from_product_shops do
    from(Api.ProductShop) |> delete_all
  end

  def get_product_shops do
    Api.ProductShop |> all
  end
end

1 个答案:

答案 0 :(得分:1)

您错过了演员电话中的:price:not_in_shop_count。试试这个:

def changeset(product_shop, params \\ %{}) do
  product_shop
  |> cast(params, [:s_id, :p_id, :price, :not_in_shop_count])
  |> validate_required([:s_id, :p_id, :price, :not_in_shop_count])
  |> unique_constraint(:s_id, name: :unique_product_shop)
end