maz

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

josevalim

Creator of Elixir

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

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:

Where Next?

Popular in Chat/Questions Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
332 30681 154
New
Fl4m3Ph03n1x
I am doing some exercises while learning Elixir using Exercism.io. Now, my objective is to do all exercises, extras included. This shoul...
New
SavagePixie
I was wondering if there are any beginner-friendly, exercise-based resources for learning Elixir out there. I’m looking for something lik...
New
OmanF
In the attached screenshot (taken from an exercise in “Programming Elixir”) can be shown that BugReport is defined as a struct that, itse...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New
boddhisattva
Greetings everyone, At my current workplace we’re evaluating different ways of building a microservices architecture for some parts rela...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
younes-alouani
I’m studying Phoenix Framework but I want to understand basics first. Which book/mooc explains better the Design of Network-based Softwar...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
zervis
Hello, I’m about to dive into web development. I was thinking about Laravel or Ruby on Rails, but then I found Phoenix. Do you recommen...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement