elixir-ecto / ecto

A toolkit for data mapping and language integrated query.
https://hexdocs.pm/ecto
Apache License 2.0
6.14k stars 1.43k forks source link

Ecto Query Error when is run from binary #3288

Closed quellcrist-falconer closed 4 years ago

quellcrist-falconer commented 4 years ago

Environment

Current behavior

This is my schema:

defmodule Voucher do
  use Ecto.Schema
  import Ecto.Changeset

  schema "voucher" do
    field(:voucher_id, :string)
    field(:event_name, :string)
    field(:customer_id, :string)
    field(:code, :string)
    field(:voucher_number, :string)
    field(:activation_time, :utc_datetime)
    field(:valid_until_time, :utc_datetime)
    field(:value, :map)
    field(:reference_key, :string)
    field(:note, :string)

    timestamps(type: :utc_datetime)
  end

  @fields ~w(voucher_id event_name customer_id code
  voucher_number activation_time valid_until_time
  value reference_key note)a

  @spec changeset(
          :invalid
          | %{optional(:__struct__) => none(), optional(atom() | binary()) => any()}
        ) :: Ecto.Changeset.t()
  def changeset(params) do
    %__MODULE__{}
    |> cast(params, @fields)
  end

  def insert(data) do
    csdata = changeset(data)

    if csdata.valid? do
      OrderapiReadmodelModel.Repo.insert(csdata)
    else
      {:error, csdata}
    end
  end
end

This is the query I'm trying to run:

map = %{
      voucher_id: "voucher id",
      customer_id: "customer id",
      code: "code",
      voucher_number: "voucher number",
      activation_time: ~U[2020-03-02 07:46:59Z],
      valid_until_time: ~U[2020-03-02 07:46:59Z],
      value: %{currency_code: "Rp", units: 1000, nanos: 25},
      reference_key: "reference key",
      note: "note"
    }
Voucher.insert(map)

When I try to run an insert query from iex -S mix, the row is inserted and this is the log:

[\"QUERY\", 32, \"OK\", \"\", [32, \"db\", 61, '61.6', 109, 115], [32, \"decode\", 61, '2.1', 109, 115], [32, \"queue\", 61, '70.8', 109, 115], [32, \"idle\", 61, '1184.1', 109, 115], 10, \"INSERT INTO \\\"voucher\\\" (\\\"activation_time\\\",\\\"code\\\",\\\"customer_id\\\",\\\"event_name\\\",\\\"note\\\",\\\"reference_key\\\",\\\"valid_until_time\\\",\\\"value\\\",\\\"voucher_id\\\",\\\"voucher_number\\\",\\\"inserted_at\\\",\\\"updated_at\\\") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING \\\"id\\\"\", 32, \"[~U[2020-03-02 07:46:59Z], \\\"code\\\", \\\"customer id\\\", \\\"VoucherRedeemed\\\", \\\"note\\\", \\\"reference key\\\", ~U[2020-03-02 07:46:59Z], %{currency_code: \\\"Rp\\\", nanos: 25, units: 1000}, \\\"voucher id\\\", \\\"voucher number\\\", ~U[2020-04-21 01:22:15Z], ~U[2020-04-21 01:22:15Z]]\"]

However, when I try to run the same query from _build/dev/rel/orderapi_readmodel/bin/orderapi_readmodel start from a binary generated by MIX_ENV=dev mix release the same query produce error and is not inserted to the database, here's the log:

[\"QUERY\", 32, \"ERROR\", \"\", [32, \"db\", 61, '44.3', 109, 115], [], [32, \"queue\", 61, '55.0', 109, 115], [32, \"idle\", 61, '749.6', 109, 115], 10, \"INSERT INTO \\\"voucher\\\" (\\\"activation_time\\\",\\\"code\\\",\\\"customer_id\\\",\\\"event_name\\\",\\\"note\\\",\\\"reference_key\\\",\\\"valid_until_time\\\",\\\"value\\\",\\\"voucher_id\\\",\\\"voucher_number\\\",\\\"inserted_at\\\",\\\"updated_at\\\") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) RETURNING \\\"id\\\"\", 32, \"[~U[2020-03-02 07:46:59Z], \\\"code\\\", \\\"customer id\\\", \\\"VoucherRedeemed\\\", \\\"note\\\", \\\"reference key\\\", ~U[2020-03-02 07:46:59Z], %{currency_code: \\\"Rp\\\", nanos: 25, units: 1000}, \\\"voucher id\\\", \\\"voucher number\\\", ~U[2020-04-21 01:24:11Z], ~U[2020-04-21 01:24:11Z]]\"]

Expected behavior

The same query should be OK whether is run from iex -S mix or from _build/dev/rel/orderapi_readmodel/bin/orderapi_readmodel start

quellcrist-falconer commented 4 years ago

My mistake, I forgot to explicitly declare {:jason, "~> 1.0"} in mix.deps