crecto icon indicating copy to clipboard operation
crecto copied to clipboard

`Repo.get` with query not using query arguments

Open faultyserver opened this issue 7 years ago • 1 comments

I've run into this a few times now. Using Repo.get with a query passed to it ignores everything about the query except for the preloads. For example:

Repo.get(Session, session_id, Query.where(valid: "true"))

Generates a query like:

SELECT * FROM acc_sessions WHERE key='...' LIMIT 1

Where the valid = "true" has not been added to the query string.

Reading the docs for Repo.get, it looks like that is intentional, but it seems rather limiting and unintuitive to me. Like in the example above, I'd like to be able to use that query parameter as a way of filtering out results that I'm not interested in, rather than having to use Repo.all(...).first.

faultyserver avatar Sep 22 '18 18:09 faultyserver

this is "by design". It's a bit confusing though. the query in Repo.get is only used for preloads. If you want to use wheres you will need to use Repo.get_by. In your case

Repo.get_by(Session, Query.where(id: session_id, valid: "true"))

repomaa avatar Dec 07 '18 16:12 repomaa