bitwalker / timex_ecto

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

(Ecto.ChangeError) value `{{YYYY, M, D}, {0, 0, 0, 0}}` for ... in insert does not match type Timex.Ecto.DateTime #32

Open markshiz opened 8 years ago

markshiz commented 8 years ago

Getting the following error:

    ** (Ecto.ChangeError) value `{{2016, 9, 3}, {0, 0, 0, 0}}` for `App.Session.expiration` in `insert` does not match type Timex.Ecto.DateTime
        (ecto) lib/ecto/query/planner.ex:33: anonymous fn/6 in Ecto.Query.Planner.fields/4
        (stdlib) lists.erl:1262: :lists.foldl/3
        (ecto) lib/ecto/query/planner.ex:21: Ecto.Query.Planner.fields/4
        (ecto) lib/ecto/repo/schema.ex:449: Ecto.Repo.Schema.dump_changes/5
        (ecto) lib/ecto/repo/schema.ex:77: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
        (ecto) lib/ecto/repo/schema.ex:477: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/9
        (ecto) lib/ecto/pool.ex:292: Ecto.Pool.with_rollback/3
        (ecto) lib/ecto/adapters/sql.ex:582: Ecto.Adapters.SQL.transaction/8
        (ecto) lib/ecto/pool.ex:244: Ecto.Pool.outer_transaction/6
        (ecto) lib/ecto/adapters/sql.ex:551: Ecto.Adapters.SQL.transaction/3

With the following model:

defmodule App.Session do
  use App.Web, :model
  use Timex

  schema "sessions" do
    field :key, :string
    field :expiration, Timex.Ecto.DateTime

    timestamps
  end

  @required_fields ~w(key expiration)
  @optional_fields ~w()

  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
  end

  def update_key_changeset(model) do
    changeset( model, %{ key: 'key', expiration: Date.now |> Date.shift(days: 90) })
  end
end

Here are my versions

$ mix hex.outdated
Dependency           Current  Latest  Requirement
phoenix              1.1.6    1.1.6   ~> 1.1.6
phoenix_ecto         2.0.2    2.0.2   ~> 2.0.2
timex                2.1.6    2.1.6   ~> 2.1.6
timex_ecto           1.1.1    1.1.1   ~> 1.1.1
bitwalker commented 8 years ago

Well that's odd, it's calling dump with {{2016, 9, 3}, {0, 0, 0, 0}} instead of a DateTime. I can add an overload to dump which just takes in such a date/time tuple and passes it through, but I wonder what that just started happening. What version of Ecto are you using?

markshiz commented 8 years ago

I'm on the latest stable version, 1.1.8

bitwalker commented 8 years ago

My tests show this is now working in the latest release, 1.1.3. I'll close this for now, but please reopen if you are still encountering issues. Thanks!

markshiz commented 8 years ago

I just updated timex_ecto and elixir to latest versions, but I'm still running into the same issue.

** (exit) an exception was raised:
    ** (Ecto.ChangeError) value `{{2016, 9, 14}, {0, 0, 0, 0}}` for `App.Session.expiration` in `insert` does not match type Timex.Ecto.DateTime
        (ecto) lib/ecto/query/planner.ex:33: anonymous fn/6 in Ecto.Query.Planner.fields/4
        (stdlib) lists.erl:1262: :lists.foldl/3
        (ecto) lib/ecto/query/planner.ex:21: Ecto.Query.Planner.fields/4
        (ecto) lib/ecto/repo/schema.ex:449: Ecto.Repo.Schema.dump_changes/5
        (ecto) lib/ecto/repo/schema.ex:77: anonymous fn/11 in Ecto.Repo.Schema.do_insert/4
        (ecto) lib/ecto/repo/schema.ex:477: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/9
        (ecto) lib/ecto/pool.ex:292: Ecto.Pool.with_rollback/3
        (ecto) lib/ecto/adapters/sql.ex:582: Ecto.Adapters.SQL.transaction/8
        (ecto) lib/ecto/pool.ex:244: Ecto.Pool.outer_transaction/6
        (ecto) lib/ecto/adapters/sql.ex:551: Ecto.Adapters.SQL.transaction/3
Dependency           Current  Latest  Requirement
phoenix              1.1.6    1.1.6   ~> 1.1.6
phoenix_ecto         2.0.2    2.0.2   ~> 2.0.2
phoenix_html         2.5.1    2.5.1   ~> 2.5.1
phoenix_live_reload  1.0.5    1.0.5   ~> 1.0.5
postgrex             0.11.1   0.11.1  >= 0.0.0
timex                2.1.6    2.1.6   ~> 2.1.6
timex_ecto           1.1.3    1.1.3   ~> 1.1.3
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.2.6
bitwalker commented 8 years ago

@markshiz Ok, I've reopened this. What version of Ecto are you running?

markshiz commented 8 years ago

ecto is the latest non-beta, 1.1.8