# 类 'app.models.User' 未映射

- **ID:** `python/sqlalchemy-orm-exc-unmapped-class-error`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

ORM 类未正确注册到 Base 或声明性基类，或者包含该类的模块在会话使用前未被导入。

## 版本兼容性

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

## 解决方案

1. **Correctly inherit from declarative base** (95% 成功率)
   ```
   Ensure the class inherits from Base = declarative_base() and import it before any session operations.
   ```
2. **Create all tables to trigger mapping** (90% 成功率)
   ```
   Call Base.metadata.create_all(engine) after defining all models to ensure mapping is established.
   ```

## 无效尝试

- **Adding decorators to the class** —  (60% 失败率)
- **Re-importing the class later** —  (50% 失败率)
