Perhaps because the conflict target is the item_id column, but your on_conflict query does not actually match on that and so it ends up trying to update columns that aren’t actually in conflict … in any case, you can also just pass the update list to on_conflict. There is no need for a whole other query:
Yea this works, but it creates a problem. I am using this feature in a shopping cart. CartItems have a has_many relationship with Order and I want the quantity update to happen only in CartItems sharing the same order id. That’s what i was trying to achieve here: on_conflict = from c in CartItems, where: c.order_id == ^attrs.order_id and c.user_id == ^attrs.user_id, update: [inc: [quantity: ^attrs.quantity]]. Doing this: on_conflict = [inc: [quantity: ^attrs.quantity]] updates the quantity irrespective of the order_id
Check to see exactly where this is failing - this error message is given whenever an UPDATE statement returns “0 rows affected”. If the insert didn’t happen, the database won’t set an id on cart_item.
@dumadi@al2o3cr I’m curious if and how this eventually is resolved? Also, is this expected behavior?
I have an on_conflict which may have “0 rows affected” when the update doesn’t occur. I guess this is also fine SQL behavior. Still Ecto will error out with a StaleEntryError.
For example: add a new entry or update an existing one but only when update_allowed is true.