maz

maz

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

Showing Posts 1 to 4

dimitarvp

dimitarvp

Try converting the UNIX seconds variable to Erlang tuple?

maz

maz OP

I tried this:

but same error. I think it has something to do with the fact that I use dynamic() because this chunk below compiles and runs(but returns inaccurate search results). I would like to use dynamic() because it makes it a lot easier to apply optional search parameters to a query.

compiles but returns incorrect results:

# compiles but does not return correct results
  def search(query_string, offset \\ 0, limit \\ 0,
    media_category,
    playlist_uuid,
    channel_uuid,
    published_after
      ) do

    {:ok, datetime} = DateTime.from_unix(published_after, :second)
    naive = DateTime.to_naive(datetime)

    query = Ecto.Query.from(mi in MediaItem,
      where: mi.updated_at >= ^naive
      )

    Logger.info("media_category: #{media_category}")
    Logger.info("naive: #{naive}")

    MediaItemsSearch.run(query, query_string)
      |> Repo.paginate(page: offset, page_size: limit)
  end

Compiles but runtime error:

#compiles but runtime error

#[debug] ** (Ecto.Query.CastError) apps/faithful_word_api/lib/faithful_word_api/controllers/api/v1.3/v13.ex:406: value `{{2019, 1, 1}, {3, 9, 29}}` in `where` cannot be cast to type :utc_datetime in query:

# from m0 in DB.Schema.MediaItem,
#   join: f1 in fragment("SELECT media_items_search.id AS id,\nts_rank(\n  media_items_search.document, plainto_tsquery(unaccent(?))\n) AS score\nFROM media_items_search\nWHERE media_items_search.document @@ plainto_tsquery(unaccent(?))\nOR media_items_search.localizedname ILIKE ?\n", ^"anderson", ^"anderson", ^"%anderson%"),
#   on: f1.id == m0.id,
#   where: m0.updated_at >= ^{{2019, 1, 1}, {3, 9, 29}},
#   select: count("*")


def search(query_string, offset \\ 0, limit \\ 0,
  media_category,
  playlist_uuid,
  channel_uuid,
  published_after
    ) do

  {:ok, datetime} = DateTime.from_unix(published_after, :second)
  naive = DateTime.to_naive(datetime)
  erl_datetime = NaiveDateTime.to_erl(datetime)

  query = Ecto.Query.from(mi in MediaItem,
    where: mi.updated_at >= ^erl_datetime
    )

  Logger.info("media_category: #{media_category}")
  Logger.info("naive: #{naive}")

  MediaItemsSearch.run(query, query_string)
    |> Repo.paginate(page: offset, page_size: limit)

# MediaItemsSearch.run(query, "thai chicken")
end

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 OP

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:

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews