Ecto Selectors Semantics

I’ve been playing around with Ecto recently, but I’m a little confused as to why the following works:

query = from u in "users",
  select: u.firstname,
  where: u.username == "tpunt"

And yet the following doesn’t work:

query = from u in "users",
  where: u.username == "tpunt"

By removing the select clause from the query, Ecto suddenly requires that we query from a model, rather than the table name (as a string).

The exact error message given is the following:

** (Ecto.QueryError) MySQL requires a model when using selector "u0" but only
the table "`users`" was given. Please specify a model or specify exactly which
fields from "u0" you desire in query:

from u in "users",
  where: u.username == "tpunt",
  select: u

Even the code example in the error message doesn’t work. The select clause must specifically choose a column (any column) in order for it to work. Can anyone explain these semantics?

Thanks.

2 Likes

@tpunt: What are you trying to achieve?
As far as I know you cannot execute filter SQL (from … where …) without action like delete, select or update.
Simplest database query like: select column from table_name; have: select and from part. You can’t execute from table_name where column ..., because you will got SQL Syntax Error.
From what I can see Ecto.QueryError is returned to secure case that developer tries to generate invalid SQL queries.