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

Nvim
Anybody know of a Pragmatic Studio 40% off coupon code for video course like Phoenix?
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
sadcad
I love the Phoenix and Elixir docs, but I always tend to learn faster when I watch a video of someone explaining things and then I implem...
New
pillaiindu
I am a VSCode and Sublime user and I know VIM, though I don’t use VIM directly but whenever I code inside Sublime or VSCode, I use Vim em...
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New
Iex.new
Hello!, I just started this week to discover Elixir. I’m a PHP-Programmer and did some sutff in Go too. The more I read about Elixir t...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
Sujit
Hi Team From the title, I am entirely new to programming and i am interested in learning elixir but not sure where to start. Can someone...
New
tom_s
Hello Elixir Community! I’m new to functional programming in general and to Elixir in particular but I’m very intrigued and would like t...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

We're in Beta

About us Mission Statement