elixir-Exto-naive_日期时间强制转换错误
在我的EXTO模型中,我需要在字段中指定:user\u timestamp从unix\u time转换的时间。 迁移:elixir-Exto-naive_日期时间强制转换错误,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,在我的EXTO模型中,我需要在字段中指定:user\u timestamp从unix\u time转换的时间。 迁移: def change do alter table(:operations) do add :user_timestamp, :naive_datetime, null: false end end 我的手术 schema "operations" do field :sum, :float field :name, :
def change do
alter table(:operations) do
add :user_timestamp, :naive_datetime, null: false
end
end
我的手术
schema "operations" do
field :sum, :float
field :name, :string
field :user_timestamp, :naive_datetime
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :sum, :user_timestamp])
|> validate_required([:sum, :user_timestamp])
|> convert_unix_time_to_ecto
end
defp convert_unix_time_to_ecto(changeset) do
put_change(changeset, :user_timestamp, Ecto.DateTime.from_unix!(changeset.changes.user_timestamp, :seconds) |> Ecto.DateTime.to_naive())
end
但是,当我尝试添加具有用户时间戳的要求时,我得到一个错误:
[error] #PID<0.492.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/v1/operations
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Ecto.Type.cast_naive_datetime/1
(ecto) lib/ecto/type.ex:761: Ecto.Type.cast_naive_datetime(1492722276)
(ecto) lib/ecto/changeset.ex:523: Ecto.Changeset.cast_field/8
(ecto) lib/ecto/changeset.ex:482: Ecto.Changeset.process_param/8
(elixir) lib/enum.ex:1325: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(elixir) lib/enum.ex:1325: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:450: Ecto.Changeset.do_cast/7
(my_app) web/models/operation.ex:25: MyApp.Operation.changeset/2
(my_app) web/models/operation.ex:32: MyApp.Operation.create_changeset/2
(my_app) web/controllers/v1/operation_controller.ex:25: MyApp.V1.OperationController.create/2
(my_app) web/controllers/v1/operation_controller.ex:1: MyApp.V1.OperationController.action/2
(my_app) web/controllers/v1/operation_controller.ex:1: MyApp.V1.OperationController.phoenix_controller_pipeline/2
(my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.instrument/4
(my_app) lib/phoenix/router.ex:261: MyApp.Router.dispatch/2
(my_app) web/router.ex:1: MyApp.Router.do_call/2
(my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
(my_app) lib/plug/debugger.ex:123: MyApp.Endpoint."call (overridable 3)"/2
(my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /home/mars/phoenix_projects/my_app/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
[错误]#运行MyApp的PID。终结点已终止
服务器:本地主机:4000(http)
请求:POST/api/v1/operations
**(退出)引发了一个异常:
**(FunctionClauseError)在exto.Type.cast\u naive\u datetime/1中没有匹配的函数子句
(exto)lib/exto/type.ex:761:exto.type.cast\u naive\u datetime(149272276)
(exto)lib/exto/changeset.ex:523:exto.changeset.cast_字段/8
(exto)lib/exto/changeset.ex:482:exto.changeset.process_param/8
(elixir)lib/enum.ex:1325:enum.“-map_reduce/3-lists^mapfoldl/2-0-”/3
(elixir)lib/enum.ex:1325:enum.“-map_reduce/3-lists^mapfoldl/2-0-”/3
(exto)lib/exto/changeset.ex:450:exto.changeset.do_cast/7
(my_app)web/models/operation.ex:25:MyApp.operation.changeset/2
(my_app)web/models/operation.ex:32:MyApp.operation.create_变更集/2
(my_app)web/controllers/v1/operation_controller.ex:25:MyApp.v1.OperationController.create/2
(my_app)web/controllers/v1/operation_controller.ex:1:MyApp.v1.OperationController.action/2
(my_app)web/controllers/v1/operation_controller.ex:1:MyApp.v1.OperationController.phoenix_controller_管道/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.instrument/4
(我的应用程序)lib/phoenix/router.ex:261:MyApp.router.dispatch/2
(my_应用程序)web/router.ex:1:MyApp.router.do_call/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.phoenix_管道/1
(my_app)lib/plug/debugger.ex:123:MyApp.Endpoint.“调用(可重写3)”/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.call/2
(plug)lib/plug/adapters/cowboy/handler.ex:15:plug.adapters.cowboy.handler.upgrade/4
(牛仔)/home/mars/phoenix_projects/my_app/deps/cowboy/src/cowboy_protocol.erl:442::cowboy_protocol.execute/4
如何更正此错误?在转换代码有机会运行之前,
转换失败
将:用户\u时间戳
从cast字段列表中保留,并在验证所需的
之前使用带有转换值的put\u change
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :sum])
|> convert_unix_time_to_ecto(params["user_timestamp"])
|> validate_required([:sum, :user_timestamp])
end
defp convert_unix_time_to_ecto(changeset, nil), do: changeset
defp convert_unix_time_to_ecto(changeset, timestamp) do
datetime =
timestamp
|> DateTime.from_unix!(:seconds)
|> DateTime.to_naive()
put_change(changeset, :user_timestamp, datetime)
end
一个选择是使用