Skip to content

no-search-all (ODW8163)

Preview (since 0.16.2.2) · Related issues · View source

Derived from the odoo linter.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for search([])/search_read([]) calls with an empty domain and no limit on a model known to hold a large number of records.

Why is this bad?

An empty domain without a limit loads all records of the model. On the tables that grow without bound in a running Odoo database — journal entries, stock moves, messages, attachments — that is a serious performance problem.

The model the call runs against is resolved from self.env["..."] (directly or through a local variable) and from the _name/_inherit of the enclosing model class. A class with no _name extends every model its _inherit names, and one listed model is enough to report. A call whose model cannot be resolved — self.env[model_name], the comodel of a relational field — is not reported.

In the code field of a cron or a server action, written in an Odoo XML data file, the model, record and records that Odoo injects resolve to the model the record's model_id refers to, and env["..."] resolves as it does in Python. There the rule also reports a search with a non-empty domain, unless the domain pins id with = or in: the code runs in a single transaction, so every record a growing domain matches is loaded and then locked by whatever the code writes, for as long as the whole batch takes. A cron posting every draft journal entry of the day that way kept deadlocking against Odoo's own auto-post cron, which posts the same rows; the fix was a limit.

Example

moves = self.env["account.move"].search([])

Use instead:

moves = self.env["account.move"].search([], limit=100)

Options

The default is the models that grow without bound in a running Odoo database. Entries are matched as globs, so account.move* covers account.move and account.move.line.