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

オープン
#944 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@cofin がすでに取り組んでいます。

2026年12月1日 から。

  • #947 @cofin による — オープン

評価

この issue はまだ評価されていません。

説明

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.

主要言語
Python
スター
110
フォーク
11
平均マージ
19時間 9分
マージ済み PR(30日)
37

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

litestar-org/sqlspec のほかの issue

litestar-org/sqlspec の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。