Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 将小时添加到日期时间字段中,以使用“外显长生不老药”_Elixir_Datetime Format_Ecto - Fatal编程技术网

Elixir 将小时添加到日期时间字段中,以使用“外显长生不老药”

Elixir 将小时添加到日期时间字段中,以使用“外显长生不老药”,elixir,datetime-format,ecto,Elixir,Datetime Format,Ecto,这是我的模型: schema "fixtures" do field :sm_id, :integer field :local_score, :integer field :visitor_score, :integer field :local_pen_score, :integer field :visitor_pen_score, :integer field :ht_score, :string field :ft_score,

这是我的模型:

schema "fixtures" do
    field :sm_id, :integer
    field :local_score, :integer
    field :visitor_score, :integer
    field :local_pen_score, :integer
    field :visitor_pen_score, :integer
    field :ht_score, :string
    field :ft_score, :string
    field :et_score, :string
    field :starting_at, Ecto.DateTime
    belongs_to :local_team, Team, foreign_key: :local_team_id
    belongs_to :visitor_team, Team, foreign_key: :visitor_team_id
    belongs_to :season, Season
    belongs_to :round, Round

    timestamps()
end
我想要的是使用以下查询获取活动装置:

def fixtures_live(query, round_) do
    now = Ecto.DateTime.utc |> Ecto.DateTime.cast!
    query
    |> join(:left, [r], f in assoc(r, :fixtures))
    |> where([r, _], r.id == ^round_.id)
    |> where([_, f], f.starting_at < ^now)
    |> where([_, f], datetime_add(f.starting_at, 2, "hour") > ^now)
    |> select([_, f], f)
end
结果是:

Compiling 11 files (.ex)

== Compilation error in file web/models/round.ex ==
** (Ecto.Query.CompileError) `Ecto.DateTime.cast!(datetime_add(f.starting_at(), 2, "hour"))` is not a valid query expression
    (ecto) expanding macro: Ecto.Query.where/3
    (sopitas) web/models/round.ex:73: Sopitas.Round.fixtures_live/2
    (ecto) expanding macro: Ecto.Query.select/3
    (sopitas) web/models/round.ex:74: Sopitas.Round.fixtures_live/2
    (elixir) expanding macro: Kernel.|>/2
    (sopitas) web/models/round.ex:74: Sopitas.Round.fixtures_live/2
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

有什么想法吗?

datetime\u add
是返回一个
:naive\u datetime
。为了能够将其与值进行比较,
now
还应该是可以转换为
:naive\u datetime
的类型
Exto.DateTime
不能强制转换为
:naive_DateTime
,但您可以在Elixir中使用新的
NaiveDateTime
模块

只要改变一下:

now = Ecto.DateTime.utc |> Ecto.DateTime.cast!
致:


除了Dogbert的答案之外,您可能还需要更改模式,以使用EXTO模式中的
:naive_datetime
:utc_datetime
类型,并使用标准库中的
NaiveDateTime
/
datetime
模块来操作值


自Ecto 2.1以来,
Ecto.DateTime
类型具有并且看起来是这样的

第一次尝试时请尝试
now=NaiveDateTime.utc\u now
(在
中没有
强制转换的那一次)
。太棒了!!,它工作得很好!,谢谢!!对不起,我想我不明白你的小费。当我创建表时,我读到建议使用:datetime创建datetime字段,转到迁移,更改:utc_datetime的:datetime,然后执行迁移。当我运行迁移时,使用mix-ecto.migrate生成的模型就是我发布的以ecto.DateTime作为字段类型的模型。
mix-ecto.migrate
不应生成任何模型,它只是针对数据库运行数据库迁移。模型可能是由Phoenix使用
mix Phoenix.gen.html
或类似工具生成的,但一旦生成,它们通常是手动维护的。你是对的,很抱歉我的混淆。但是,我使用您建议的方式添加了一个新字段:utc_time,但是当我将新字段添加到模型中时,它会给我以下消息:==文件web/models/fixture.ex中的编译错误=**(ArgumentError)字段的无效或未知类型Naive_DateTime:feedtime_start和使用DateTime时的相同消息,这就是为什么我要使用EXTO.DateTime
Compiling 11 files (.ex)

== Compilation error in file web/models/round.ex ==
** (Ecto.Query.CompileError) `Ecto.DateTime.cast!(datetime_add(f.starting_at(), 2, "hour"))` is not a valid query expression
    (ecto) expanding macro: Ecto.Query.where/3
    (sopitas) web/models/round.ex:73: Sopitas.Round.fixtures_live/2
    (ecto) expanding macro: Ecto.Query.select/3
    (sopitas) web/models/round.ex:74: Sopitas.Round.fixtures_live/2
    (elixir) expanding macro: Kernel.|>/2
    (sopitas) web/models/round.ex:74: Sopitas.Round.fixtures_live/2
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
now = Ecto.DateTime.utc |> Ecto.DateTime.cast!
now = NaiveDateTime.utc_now