Ecto.Repo.get_by - Are shared options supported?

I’m trying to use the shared options in Ecto to suppress the query log, but I can’t seem to figure out the syntax for Ecto.Repo.get_by. Does anyone know it?

Shared options

Almost all of the repository operations below accept the following options:

:timeout - The time in milliseconds to wait for the query call to finish, :infinity will wait indefinitely (default: 15000);
:pool_timeout - The time in milliseconds to wait for calls to the pool to finish, :infinity will wait indefinitely (default: 5000);
:log - When false, does not log the query
Such cases will be explicitly documented as well as any extra option.

Repo.get_by(MySchema, name: "lookup name", log: false)

This syntax doesn’t work, but it does for Ecto.Repo.get:

Repo.get(MySchema, 1, log: false)

The reason is because Repo.get_by/3 expects three arguments, queryable, which is your MySchema, clauses, which in your example is name: "lookup name", log: false and finally, an optional opts, which in your example, you’re not supplying.

You need to separate the keywords of clauses from opts

The correct way would be:

Repo.get_by(MySchema, [name: "lookup name"], log: false)
1 Like