python
type_error
ai_generated
true
sqlalchemy.exc.ArgumentError: Invalid sort expression: 'invalid_column'
ID: python/sqlalchemy-invalid-sort-expression
80%Fix Rate
83%Confidence
0Evidence
2025-10-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Passing a string that does not correspond to a valid column or expression to the order_by method.
generic中文
向 order_by 方法传递了一个不对应有效列或表达式的字符串。
Workarounds
-
98% success Use the column attribute directly
session.query(User).order_by(User.name).all()
-
90% success Use text() for raw SQL expressions
from sqlalchemy import text session.query(User).order_by(text('name DESC')).all()
Dead Ends
Common approaches that don't work:
-
Using the string as is without validation
100% fail
The error will persist.
-
Renaming the column to match the string
50% fail
This changes the database schema unnecessarily.