SQLAlchemy:关系将复制与关系冲突的列

2024-06-23 19:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下映射

class Socio(Base):
    __tablename__ = "socio"
    member_id = Column(Integer, primary_key=True)
    work_profile = relationship(
        "Laboral",
        uselist=False,
        primaryjoin="foreign(Socio.member_id) == Laboral.member_id",
    )


class Familiar(Base):
    __tablename__ = "familiar"
    member_id = Column(Integer, ForeignKey("socio.member_id"), primary_key=True)
    family_code = Column(Integer, primary_key=True)
    socio = relationship("Socio", backref="familiares")
    work_profile = relationship(
        "Laboral",
        uselist=False,
        primaryjoin="and_("
        "foreign(Familiar.member_id) == Laboral.member_id, "
        "foreign(Familiar.family_code) == Laboral.family_code"
        ")",
    )


class Laboral(Base):
    member_id = Column(Integer, ForeignKey("socio.member_id"), primary_key=True)
    family_code = Column(Integer, primary_key=True)
    __tablename__ = "laboral"
    __table_args__ = (
        ForeignKeyConstraint(
            ("member_id", "family_code"), ("familiar.member_id", "familiar.family_code")
        ),
    )

在尝试对Social或Friends执行查询时,我收到以下警告:

SAWarning: relationship 'Familiar.work_profile' will copy column laboral.member_id to 
column familiar.member_id, which conflicts with relationship(s): 'Familiar.socio' (copies
 socio.member_id to familiar.member_id), 'Socio.familiares' (copies socio.member_id to 
familiar.member_id). If this is not the intention, consider if these relationships should
 be linked with back_populates, or if viewonly=True should be applied to one or more if 
they are read-only. For the less common case that foreign key constraints are partially 
overlapping, the orm.foreign() annotation can be used to isolate the columns that should 
be written towards.   To silence this warning, add the parameter 
'overlaps="familiares,socio"' to the 'Familiar.work_profile' relationship.

我有点新使用sqlalchemy进行db映射,我更习惯于Django ORM的魔力

如何解决此警告并纠正所有关系


Tags: thetokeyidtruecodecolumninteger

热门问题