maz
Comparing :utc_datetime and DateTime in Ecto 3 query
I’m getting this error:
## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size
from this line in an Ecto query:
dynamic([mi], where: mi.updated_at >= ^datetime or ^conditions)
mi.updated_at is defined as: timestamps(type: :utc_datetime)
The incoming published_after is a unix epoch value which I convert to a DateTime with from_unix()
Pretty new to dates in Elixir in general, so I’m not sure what is going on. Any help?
Michael
conditions =
if published_after do
{:ok, datetime} = DateTime.from_unix(published_after, :second)
Logger.info("published_after: #{published_after}")
Logger.info("datetime: #{datetime}")
## log output:
## [info] published_after: 1546312169
## [info] datetime: 2019-01-01 03:09:29Z
## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size
dynamic([mi], where: mi.updated_at >= ^datetime or ^conditions)
# updated_at defined as:- timestamps(type: :utc_datetime)
else
conditions
end
Most Liked
josevalim
Ecto 3 does not allow erl_datetime anymore, since we have proper calendar types. You have to pass the naive_datetime or the datetime directly instead.
maz
Thanks, confirmed and working. I had a confluence of two bugs: using the wrong time format and incorrect syntax in the dynamic() macro.
For future readers(I ANDed the conditions, YMMV):
conditions =
if published_after do
{:ok, datetime} = DateTime.from_unix(published_after, :second)
naive = DateTime.to_naive(datetime)
dynamic([mi], mi.updated_at >= ^naive and ^conditions)
else
conditions
end
query = Ecto.Query.from(mi in MediaItem, where: ^conditions)
MediaItemsSearch.run(query, query_string)
|> Repo.paginate(page: offset, page_size: limit)
More info:
Popular in Chat/Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








