bitwalker / timex_ecto

An adapter for using Timex DateTimes with Ecto
MIT License
162 stars 68 forks source link

Having difficulties updating a record #74

Closed rafbgarcia closed 6 years ago

rafbgarcia commented 7 years ago

I'm trying to update the following record:

now = Timex.local

Repo.get(Order, order_id)
|> Order.changeset(%{state: "confirmed", confirmed_at: now})
|> Repo.update!

But I'm getting this error could not encode date/time: {{{2017, 11, 5}, {3, 8, 1, 64322}}, "America/Sao_Paulo"}

Because it's trying to execute this sql

[debug] QUERY ERROR db=0.4ms
UPDATE "orders" SET "confirmed_at" = $1, "state" = $2, "updated_at" = $3 WHERE "id" = $4 [{{{2017, 11, 5}, {3, 8, 1, 64322}}, "America/Sao_Paulo"}, "confirmed", {{2017, 11, 5}, {5, 8, 1, 0}}, 1]

My schema has the following fields:

schema "orders" do
    [...]
    field :state, :string
    field :confirmed_at, Timex.Ecto.DateTimeWithTimezone

    timestamps()
  end

And I'm using these options for the timestamp:

@timestamps_opts [type: Timex.Ecto.TimestampWithTimezone,
        autogenerate: {Timex.Ecto.TimestampWithTimezone, :autogenerate, []}]

I'm using

{:timex, "~> 3.1.24"},
{:timex_ecto, "~> 3.2.1"}

Am I missing something?

joeyrosztoczy commented 6 years ago

@rafbgarcia were you able to resolve this issue? I'm getting the same error as well

rafbgarcia commented 6 years ago

Yup, ended up with

      @timestamps_opts [type: Timex.Ecto.DateTime,
                    autogenerate: {Timex.Ecto.DateTime, :autogenerate, []}]