python
module_error
ai_generated
true
类 'app.models.User' 未映射
sqlalchemy.orm.exc.UnmappedClassError: Class 'app.models.User' is not mapped
ID: python/sqlalchemy-orm-exc-unmapped-class-error
80%修复率
84%置信度
0证据数
2024-07-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
ORM 类未正确注册到 Base 或声明性基类,或者包含该类的模块在会话使用前未被导入。
English
The ORM class has not been properly registered with a Base or declarative base, or the module containing the class was not imported before session usage.
解决方案
-
95% 成功率 Correctly inherit from declarative base
Ensure the class inherits from Base = declarative_base() and import it before any session operations.
-
90% 成功率 Create all tables to trigger mapping
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% 失败