python
config_error
ai_generated
true
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.addresses - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument.
ID: python/sqlalchemy-ambiguous-foreign-key
80%Fix Rate
85%Confidence
0Evidence
2024-06-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Multiple foreign keys between two tables without explicit join condition.
generic中文
两个表之间存在多个外键,未指定显式连接条件。
Workarounds
-
95% success Specify 'foreign_keys' argument in relationship.
class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) addresses = relationship('Address', foreign_keys='Address.user_id') -
90% success Use primaryjoin explicitly.
addresses = relationship('Address', primaryjoin='User.id == Address.user_id')
Dead Ends
Common approaches that don't work:
-
Removing one foreign key constraint from the database.
50% fail
Destructive and may break data integrity; not necessary.
-
Setting primaryjoin with a string that doesn't match columns.
70% fail
String must be a valid SQL expression; typos cause errors.