python config_error ai_generated true

无法确定父/子表之间的关系 'User.addresses' 的连接条件 - 没有外键链接这些表。确保引用列与外键关联,或指定 'primaryjoin' 表达式。

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship 'User.addresses' - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

ID: python/sqlalchemy-missing-ondelete-option

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-03-11首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

表之间定义关系时没有外键约束,或外键未正确声明。

English

A relationship is defined without a foreign key constraint between the tables, or the FK is not properly declared.

generic

解决方案

  1. 95% 成功率
    class Address(Base):
        __tablename__ = 'addresses'
        user_id = Column(Integer, ForeignKey('users.id'))
    class User(Base):
        addresses = relationship('Address')
  2. 80% 成功率
    class User(Base):
        addresses = relationship('Address', primaryjoin='User.id == Address.user_id')

无效尝试

常见但无效的做法:

  1. 60% 失败

    It's just a regular column; no constraint.

  2. 50% 失败

    May cause ambiguous joins.