python type_error ai_generated true

sqlalchemy.exc.ArgumentError: 无效的排序表达式: 'invalid_column'

sqlalchemy.exc.ArgumentError: Invalid sort expression: 'invalid_column'

ID: python/sqlalchemy-invalid-sort-expression

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2025-10-05首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

向 order_by 方法传递了一个不对应有效列或表达式的字符串。

English

Passing a string that does not correspond to a valid column or expression to the order_by method.

generic

解决方案

  1. 98% 成功率 Use the column attribute directly
    session.query(User).order_by(User.name).all()
  2. 90% 成功率 Use text() for raw SQL expressions
    from sqlalchemy import text
    session.query(User).order_by(text('name DESC')).all()

无效尝试

常见但无效的做法:

  1. Using the string as is without validation 100% 失败

    The error will persist.

  2. Renaming the column to match the string 50% 失败

    This changes the database schema unnecessarily.