builder: order_by("col", "asc") silently treats the direction as a column

Đang mở
#944 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@cofin đang làm issue này rồi.

Từ ngày 1/12/2026.

  • #947 của @cofin — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

enhancement

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:

  1. Raise SQLBuilderError when a positional item is exactly asc or desc (case-insensitive), with a message pointing to desc=True or "col DESC".
  2. 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.

Ngôn ngữ chính
Python
Star
110
Fork
11
Merge trung bình
19 giờ 9 phút
Pull request đã merge (30 ngày)
37

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của litestar-org/sqlspec

Tất cả issue của litestar-org/sqlspec

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.