python
config_error
ai_generated
true
sqlalchemy.exc.ArgumentError: relationship 'User.posts' expects a class or a mapper argument (received: <class 'str'>)
ID: python/sqlalchemy-invalid-relationship-argument
80%Fix Rate
83%Confidence
0Evidence
2024-05-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.4.x | active | — | — | — |
| 2.0.x | active | — | — | — |
Root Cause
Passing a string to relationship() without proper string resolution context, e.g., using a non-existent class name.
generic中文
向 relationship() 传递了字符串,但未在正确的字符串解析上下文中,例如使用了不存在的类名。
Workarounds
-
95% success
Ensure the class is defined before the relationship: class Post(Base): ...; class User(Base): posts = relationship(Post)
-
85% success
Use string with full module path if using declarative: posts = relationship('myapp.models.Post')
Dead Ends
Common approaches that don't work:
-
80% fail
If the class is not yet defined, it will raise NameError.
-
90% fail
NameError occurs because the class is not in scope.