# sqlalchemy.exc.InvalidRequestError: 无法确定父/子表之间的连接条件，关系 User.addresses

- **ID:** `python/sqlalchemy-cannot-determine-join-condition`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

SQLAlchemy 无法自动推断两个模型之间的外键关系，通常是由于缺少或模糊的外键。

## 版本兼容性

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

## 解决方案

1. **Explicitly define the foreign_keys argument in the relationship** (95% 成功率)
   ```
   class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    addresses = relationship('Address', foreign_keys='Address.user_id')
   ```
2. **Use primaryjoin to specify the join condition** (90% 成功率)
   ```
   addresses = relationship('Address', primaryjoin='User.id == Address.user_id')
   ```

## 无效尝试

- **Adding a random foreign key column** — The column may not match the intended relationship, causing incorrect joins. (70% 失败率)
- **Removing the relationship entirely** — This breaks the ORM association and required functionality. (90% 失败率)
