Ecto.QueryError: keyword lists can only be interpolated at the top level of where, having, distinct, order_by, update or a join's on in query

  def update_record(changeset, key_atom, identifier, table_name, attempts \\ 1, glitch \\ :pass)

  def update_record(changeset, key_atom, identifier, table_name, 4 = attempts, glitch) do
    {:error,
     glitch: glitch,
     table_name: table_name,
     identifier: identifier,
     key_atom: key_atom,
     changeset: changeset,
     total_attempts: attempts - 1}
    |> LogBook.register_error(__MODULE__, 89)
  end

  def update_record(changeset, key_atom, identifier, table_name, attempts, _glitch) do
    update_report =
      try do
        Repo.transaction(fn ->
          from(record in table_name,
            where: field(record, ^key_atom) == ^identifier
          )
          |> Repo.update_all(set: changeset)
        end)
      rescue
        error ->
          update_record(changeset, key_atom, identifier, table_name, attempts + 1, error)
      end

    confirm_successful_update(table_name, update_report)
  end


  def update_inventory(order_uid, amount_sold) do
    %{machine_part_no: part_no} = Sales.get_purchase_order(order_uid)

    DB_API.update_record(
      [inc: [quantity: amount_sold * -1.0]],
      :part_no,
      part_no,
      Inventory
    )
  end

Executing update_inventory(order_uid, amount_sold) results in this error:

{:error, [glitch: %Ecto.QueryError{message: "deps/ecto/lib/ecto/repo/queryable.ex:132: keyword lists can only be interpolated at the top level of where, having, distinct, order_by, update or a join's on in query:\n\nfrom r0 in Warehouse.Inventory,\n where: r0.part_no == ^\"NH9IL4LOZ\",\n update: [set: [inc: ^[quantity: 121]]]\n"}, table_name: Warehouse.Inventory, identifier: "NH9IL4LOZ", key_atom: :part_no, changeset: [inc: [quantity: 121]], total_attempts: 3]}

What is this error message attempting to convey and how is the issue remedied?

here is the error, it should be
set: ^[quantity: 121]
or
inc: ^[quantity: 121]
but not both


  1. quantity: 121 ↩︎

2 Likes