Basics
This check is disabled by default.
Learn how to enable it via .credo.exs.
This check has a base priority of higher and works with any version of Elixir.
Explanation
Do not call Repo.preload after loading records with Repo.one or
Repo.all. Prefer composing the preload into the Ecto query so the
preload intent stays visible at the query boundary.
Query-level preloads keep retrieval and association requirements together, so callers can see the complete database operation and the query can be reviewed or composed as one unit. They also avoid hiding an additional database operation after the Repo read has already returned.
Examples
Avoid:
query
|> Repo.one()
|> Repo.preload([:message])Prefer:
query
|> preload([:message])
|> Repo.one()The same rule applies when a local helper hides the Repo.preload call:
query
|> Repo.one()
|> preload_message()Prefer:
query
|> preload([:message])
|> Repo.one()Notes
This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.
Options
This check has no check-specific options. Configure it with an empty option list.
Usage
Add this check to Credo's checks: list in .credo.exs:
%{
configs: [
%{
name: "default",
checks: [
{Bylaw.Credo.Check.Ecto.NoRepoPreloadAfterQuery, []}
]
}
]
}Check-Specific Parameters
There are no specific parameters for this check.
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.