python
config_error
ai_generated
true
sqlalchemy.exc.ArgumentError: relationship 'User.posts' 期望一个类或映射器参数(收到:<class 'str'>)
sqlalchemy.exc.ArgumentError: relationship 'User.posts' expects a class or a mapper argument (received: <class 'str'>)
ID: python/sqlalchemy-invalid-relationship-argument
80%修复率
83%置信度
0证据数
2024-05-11首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.4.x | active | — | — | — |
| 2.0.x | active | — | — | — |
根因分析
向 relationship() 传递了字符串,但未在正确的字符串解析上下文中,例如使用了不存在的类名。
English
Passing a string to relationship() without proper string resolution context, e.g., using a non-existent class name.
解决方案
-
95% 成功率
Ensure the class is defined before the relationship: class Post(Base): ...; class User(Base): posts = relationship(Post)
-
85% 成功率
Use string with full module path if using declarative: posts = relationship('myapp.models.Post')
无效尝试
常见但无效的做法:
-
80% 失败
If the class is not yet defined, it will raise NameError.
-
90% 失败
NameError occurs because the class is not in scope.