Im on phoenix tutorial but using the Tds adapter (SQL Server), at the shopping cart behavior. The code fails using the on_conflict option for insert, here is the code
def add_item_to_cart(%Cart{} = cart, %Catalog.Product{} = product) do
%CartItem{quantity: 1, price_when_carted: product.price}
|> CartItem.changeset(%{})
|> Ecto.Changeset.put_assoc(:cart, cart)
|> Ecto.Changeset.put_assoc(:product, product)
|> Repo.insert(
on_conflict: [inc: [quantity: 1]],
conflict_target: [:cart_id, :product_id]
)
end
The error is Tds adapter supports only on_conflict: :raise
I'm trying to follow the tutorial and want to know if it is a dead-end or if there is an alternative to achieve the same.
There are three alternatives.
on_conflict: :raise
), rescue
the raised exception, and execute another query that would increase quantity
by 1
from rescue
clauseselect
query for [:cart_id, :product_id]
and if it returned an empty result, call add_item_to_cart/2
, otherwise increase quantity
by 1