builder: explain why boolean projections are rejected in sql.select() string columns

Aperta
#943 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@cofin ci sta già lavorando.

Dal 1/12/2026.

  • #947 di @cofin — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

enhancement

Summary

Passing a boolean projection as a string to sql.select(...) raises SQLBuilderError: Invalid column expression: '<text>' with no reason and no pointer to the supported form. The equivalent table-reference error already explains what to pass instead, so this one is harder to act on than it needs to be.

Reproduction

sqlspec==0.74.2

from sqlspec import sql

sql.select("u.id", "u.login_count > 0 AS has_logged_in").from_("users u")
sql.select("u.id", "(u.is_active AND u.is_verified) AS can_sign_in").from_("users u")

Both raise:

SQLBuilderError: Invalid column expression: 'u.login_count > 0 AS has_logged_in'
SQLBuilderError: Invalid column expression: '(u.is_active AND u.is_verified) AS can_sign_in'

Nearby forms build:

sql.select("u.id", "u.login_count + 1 AS next_login")
sql.select("u.id", "CASE WHEN u.login_count > 0 THEN TRUE ELSE FALSE END AS has_logged_in")
sql.select("u.id", sql.raw("u.login_count > 0 AS has_logged_in"))

Why it is confusing

A boolean projection is ordinary SQL on PostgreSQL, DuckDB, SQLite, and MySQL, and the arithmetic form of the same string is accepted. The message repeats the input without saying that a top-level comparison or AND/OR is what was rejected, so the natural reading is that the expression failed to parse. The usual reaction is to wrap the predicate in CASE WHEN ... THEN TRUE ELSE FALSE END, when sql.raw(...) would have kept the original expression.

Proposed change

When a string column is rejected for a top-level predicate or connector, say so and name the supported form, in the same style as the table-reference message. For example:

Invalid column expression 'u.login_count > 0 AS has_logged_in': column text cannot be a
top-level comparison or AND/OR expression. Pass an expression instead, such as
sql.raw('u.login_count > 0 AS has_logged_in'), or wrap it in CASE WHEN.

A short note in the query builder docs on boolean projections would cover the same gap.

Separately, it is worth deciding whether the restriction should apply to projections at all, since the same predicate is accepted inside CASE WHEN and through sql.raw. If it stays, the clearer message is enough.

Lingua principale
Python
Stelle
110
Fork
11
Merge medio
19h 9m
PR unite (30g)
37

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di litestar-org/sqlspec

Tutte le issue di litestar-org/sqlspec

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.