我正在尝试更新mysql表中的一些数据。我正在使用Ecto执行数据库操作。
User表与ItemsPurchased
有关联,has_many
关联和belongs_to
如下:
schema "sales_orders" do
field(:total_bill, :float)
field(:quantity, :integer)
field :sales_order_id, :integer
has_many :items_purchased, Orders.ItemsPurchased, on_replace: :delete
timestamps()
end
schema "items_purchased" do
field :item_id, :string
field :product, :string
field :description, :string
field :picture, :string
field :quantity, :integer
field :required_total_bill, :decimal
belongs_to(:sales_order, Orders.SalesOrder)
timestamps()
end
schema "sales_orders" do
field(:quantity, :integer)
field(:so_date, :utc_datetime)
field :sales_order_id, :integer
has_many :sales_order_line_items,
Orders.SalesOrderLineItem, on_replace: :delete
timestamps()
end
这里的问题是,当我尝试更新items_purchased时,ecto变更集会插入空白数据。变更集如下:
Ecto.Changeset.put_assoc(inserted_so, :items_purchased, items_purchased)
#Ecto.Changeset<
action: nil,
changes: %{
total_bill: 300.0,
items_purchased: [
#Ecto.Changeset<action: :replace, changes: %{}, errors: [],
data: #Orders.ItemsPurchased<>, valid?: true>,
#Ecto.Changeset<
action: :insert,
changes: %{
"description" => "white",
"item_id" => "88-1",
"product" => "tee",
"quantity" => 50,
"required_total_bill" => "500"
},
errors: [],
data: #Orders.ItemsPurchased<>,
valid?: true
>
]
},
errors: [],
data: #Orders.SalesOrder<>,
valid?: true
>
Repo.update的输出与put_assoc一起是:
** (Ecto.StaleEntryError) attempted to delete a stale struct:
任何人都可以让我知道这是怎么回事吗?
谢谢, S