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

- **ID:** `python/sqlalchemy-invalid-sort-expression`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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

## 无效尝试

- **Using the string as is without validation** — The error will persist. (100% 失败率)
- **Renaming the column to match the string** — This changes the database schema unnecessarily. (50% 失败率)
