builder: order_by("col", "asc") silently treats the direction as a column
Maintainers usually reply within 1 day
Assessment
This issue has not been assessed yet.
Description
Summary
order_by("column", "asc") and order_by("column", "desc") build without error and treat the direction word as a second column. The mistake only surfaces when the database rejects the query, and with "desc" the caller's intended sort direction is dropped.
Reproduction
sqlspec==0.74.2
from sqlspec import sql
sql.select("u.id").from_("users u").order_by("u.name", "asc").build(dialect="postgres").sql
sql.select("u.id").from_("users u").order_by("u.name", "desc").build(dialect="postgres").sql
Output:
SELECT "u"."id" AS "id" FROM "users" AS "u" ORDER BY "u"."name", "u"."asc"
SELECT "u"."id" AS "id" FROM "users" AS "u" ORDER BY "u"."name", "u"."desc"
PostgreSQL then fails with an undefined-column error for u.asc / u.desc. If the query has a single unqualified table that happens to have such a column, it would run and sort by the wrong thing.
The supported forms work as expected:
order_by("u.name", desc=True) # ORDER BY "u"."name" DESC
order_by("u.name DESC") # ORDER BY "u"."name" DESC
Why it happens
The signature is order_by(*items, desc=False), so every positional string is an order item. (column, direction) is a common calling convention in other query APIs, which makes this an easy slip, and nothing in the builder flags it.
Proposed change
Either of these would catch it at build time:
- Raise
SQLBuilderErrorwhen a positional item is exactlyascordesc(case-insensitive), with a message pointing todesc=Trueor"col DESC". - Accept a trailing
"asc"/"desc"as the direction for the preceding item.
Option 1 is the smaller change and adds no new calling convention. A column that is really named asc or desc can still be passed quoted or as a column expression.
- Dominant language
- Python
- Stars
- 110
- Forks
- 11
- Avg merge
- 19h 9m
- Merged PRs (30d)
- 37
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from litestar-org/sqlspec
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
litestar-org/sqlspec#946 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
litestar-org/sqlspec#945 ·
Maintainers usually reply within 1 day
-
builder: explain why boolean projections are rejected in sql.select() string columnsPossibly taken @cofin claimed this 1 day ago. Openenhancement
litestar-org/sqlspec#943 · 1 assignee ·
Maintainers usually reply within 1 day
-
feat(litestar): support Litestar v3 compatibility and DI changesPossibly taken @cofin claimed this 14 days ago. Openenhancement
litestar-org/sqlspec#926 · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
litestar-org/sqlspec#509 ·
Maintainers usually reply within 1 day
All issues in litestar-org/sqlspec
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
nael5x/creative-ai#130 ·
-
Make the "%" unit spacing the same in the web and in A10Possibly taken @kragent66-glitch claimed this today. Openbug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
maiychrus25/OpenTea-DX-OS#226 · 1 comment ·
-
ci severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
TMHSDigital/Spackle#205 ·
Maintainers usually reply within 1 day
-
pkg:lgm type:chore upstream-gaussx
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
jejjohnson/pyrox#610 ·
Maintainers usually reply within 1 day