elixir - Timex - using datetime with a migration -
i've got following module :
defmodule soundbuttonsapi.accounts.session use ecto.schema use timex import ecto.changeset schema "accounts_sessions" field :expires_at, timex.ecto.datetime, null: false field :token, :string, null: false field :user_id, :id, null: false timestamps() end def changeset(model, params \\ :empty) model |> cast(params, [:user_id]) |> set_token() |> set_expires_at() |> validate_required([:user_id, :token, :expires_at]) end defp set_token(changeset) put_change(changeset, :token, uuid.uuid4()) end defp set_expires_at(changeset) put_change(changeset, :expires_at, timex.shift(timex.now(), days: 1)) end end
and associated migration :
defmodule soundbuttonsapi.repo.migrations.createsoundbuttonsapi.accounts.session use ecto.migration def change create table(:accounts_sessions) add :token, :string add :expires_at, :utc_datetime add :user_id, references(:accounts_users, on_delete: :nothing) timestamps() end create index(:accounts_sessions, [:user_id]) create index(:accounts_sessions, [:token]) end end
but insertion attempt yield following error : (caseclauseerror) no case clause matching: {{{2017, 4, 8}, {10, 8, 58, 947735}}}
any idea how fix ?
i using versions 3.1 of both timex , timex_ecto.
edit : full stacktrace of error :
stacktrace: (ecto) lib/ecto/adapters/postgres/datetime.ex:40: ecto.adapters.postgres.typemodule.encode_params/3 (postgrex) lib/postgrex/query.ex:45: dbconnection.query.postgrex.query.encode/3 (db_connection) lib/db_connection.ex:1071: dbconnection.describe_run/5 (db_connection) lib/db_connection.ex:1142: anonymous fn/4 in dbconnection.run_meter/5 (db_connection) lib/db_connection.ex:1199: dbconnection.run_begin/3 (db_connection) lib/db_connection.ex:584: dbconnection.prepare_execute/4 (ecto) lib/ecto/adapters/postgres/connection.ex:93: ecto.adapters.postgres.connection.execute/4 (ecto) lib/ecto/adapters/sql.ex:243: ecto.adapters.sql.sql_call/6 (ecto) lib/ecto/adapters/sql.ex:562: ecto.adapters.sql.struct/7 (ecto) lib/ecto/repo/schema.ex:467: ecto.repo.schema.apply/4 (ecto) lib/ecto/repo/schema.ex:205: anonymous fn/13 in ecto.repo.schema.do_insert/4 (sound_buttons_api) lib/sound_buttons_api/web/controllers/session_controller.ex:26: soundbuttonsapi.web.sessioncontroller.create/2 (sound_buttons_api) lib/sound_buttons_api/web/controllers/session_controller.ex:1: soundbuttonsapi.web.sessioncontroller.action/2 (sound_buttons_api) lib/sound_buttons_api/web/controllers/session_controller.ex:1: soundbuttonsapi.web.sessioncontroller.phoenix_controller_pipeline/2 (sound_buttons_api) lib/sound_buttons_api/web/endpoint.ex:1: soundbuttonsapi.web.endpoint.instrument/4 (phoenix) lib/phoenix/router.ex:277: phoenix.router.__call__/1 (sound_buttons_api) lib/sound_buttons_api/web/endpoint.ex:1: soundbuttonsapi.web.endpoint.plug_builder_call/2 (sound_buttons_api) lib/sound_buttons_api/web/endpoint.ex:1: soundbuttonsapi.web.endpoint.call/2 (phoenix) lib/phoenix/test/conn_test.ex:224: phoenix.conntest.dispatch/5 test/web/controllers/session_controller_test.exs:21: (test)
edit 2 : culprit (?) code :
def create(conn, %{"user" => user_params}) user = repo.get_by!(soundbuttonsapi.accounts.user, email: user_params["email"]) {:ok, %session{} = session} <- accounts.create_session(%{user_id: user.id}) # line 26 conn |> put_status(:created) |> put_resp_header("location", session_path(conn, :show, session)) |> render("show.json", session: session) end end
and create_session :
def create_session(attrs \\ %{}) %session{} |> session.changeset(attrs) |> repo.insert() end
Comments
Post a Comment