python config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-03-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 60% fail

    It's just a regular column; no constraint.

  2. 50% fail

    May cause ambiguous joins.