SQLAlchemy:如何在一对一关系中使用“uselist”
我是SQLAlchemy的新手,我正在尝试在数据库中设置SQLAlchemy:如何在一对一关系中使用“uselist”,sqlalchemy,foreign-keys,relationship,Sqlalchemy,Foreign Keys,Relationship,我是SQLAlchemy的新手,我正在尝试在数据库中设置relationship()。我正在查看SQLAlchemy文档。我还被告知,我必须使用back\u填充,而不是backref 我正在努力理解这一点 为什么uselist=False仅用于Parent模型的child关系?我认为在真正的一对一关系中,子模型在其父关系中也会有uselist=False class Parent(Base): __tablename__ = 'parent' id = Column(Intege
relationship()
。我正在查看SQLAlchemy文档。我还被告知,我必须使用back\u填充
,而不是backref
我正在努力理解这一点
为什么uselist=False
仅用于Parent
模型的child
关系?我认为在真正的一对一关系中,子
模型在其父
关系中也会有uselist=False
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child = relationship("Child", uselist=False, back_populates="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", uselist=False, back_populates="child")
我想我不明白uselist
是如何工作的。文档中说它“表示在关系的“多”端放置标量属性而不是集合。”这是什么意思
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child = relationship("Child", uselist=False, back_populates="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", uselist=False, back_populates="child")