Skip to content

Raw SQL in a filter

A raw condition in Repo's filter stays inside its parentheses, a fragment that could leave them is refused, and clippy names every raw-text call.

filter takes what any SeaORM condition is, and sea-query’s Expr::cust builds the same Expr a column’s .eq() does, so no type tells raw text from a condition. Repo holds it where it lands instead: your condition goes into the statement as one parenthesized operand beside the ability’s, and its text, read as Postgres reads it, has to close every parenthesis, string and quoted identifier it opens, with no comment in it. A fragment that does not is refused before any statement is sent:

what reaches Postgres
-- .filter(Column::Title.eq(name))
WHERE "org_id" = $1 AND ("posts"."title" = $2)
-- .filter(Expr::cust("1 = 1) OR (1 = 1"))
-- nothing: the query is refused

The refused query runs nothing and answers the opaque 500 every unexpected database failure answers, and one warn on nest_rs::orm names the entity and why it was refused (reason="unbalanced_parenthesis"), never the text. Text holding an OR stays inside its parentheses, so a sea_query::Enum literal or a hand-written impl Iden whose text reaches past its column still reads within what the ability allows. A page’s extra condition goes through the same check, and so does an order by field; a write refuses a value whose enum type name would leave its quotes, alone or at any depth of an array.

What you write when no typed builder reaches it keeps working, with nothing new to write: a function call, a full-text search, a string holding a parenthesis. Two spellings are refused although Postgres reads them: dollar quoting ($$…$$), and a backslash in a plain '…' string, which escapes the next character wherever a server sets standard_conforming_strings off. Write E'…', or better, bind the value.

Clippy names the same fragments where you write them. The clippy.toml nestrs new writes lists Expr::cust and every other call through which sea-query writes text unescaped, and nestrs run lint runs it with -D warnings. Write the condition with sea-query’s typed builders (Func::lower, PgFunc::to_tsquery, Condition), whose values are bound. PgFunc’s JSON builders are the exception, a JSON path being written as text, so json_value, json_query, json_exists and json_table are on the list too. Where only raw text can say it, the opening is one line a review finds, and a value goes through cust_with_values, never into the text:

crates/features/src/posts/service.rs
#[expect(
clippy::disallowed_methods,
reason = "full-text search over a generated column; the query is a bound value"
)]
let matching = Expr::cust_with_values("search @@ plainto_tsquery($1)", [q]);

A derive’s expansion sits beside the item carrying it, where an #[expect] on that item does not reach. SeaORM’s #[sea_orm(select_as = …, save_as = …, save_array_as = …)] expands to cast_as, so the entity’s module takes the opening, at the top of its file:

crates/features/src/tags/entity.rs
#![expect(
clippy::disallowed_methods,
reason = "`select_as` and `save_as` expand to `cast_as`, on a constant type name"
)]

A DeriveActiveEnum on a native enum with no rs_type expands to sea_query::Enum the same way, which takes clippy::disallowed_types beside it; with rs_type = "String" it expands to nothing the list names and needs no opening.

Clippy names calls and types, so a sea_query::Enum literal through its full path or a glob, and a hand-written impl Iden whose quoted borrows its text, pass it; Repo’s check is what holds those.

A project that started before this file has none: copy it from a new scaffold. A migration stays free to write raw DDL: crates/migrations holds an empty clippy.toml of its own, since no request’s value reaches it.