由于 sqlalchemy 这玩意儿不是特别经常使用,偶然提起的时候想写个多对多关系还搜索了半天。因而趁机作个笔记。python
db.ForeginKey
的参数是<表名>.<键名>
,而不是<类名>.<字段名>
,务必注意这个区别。sql
back_populates
是更推荐的写法。code
多对多关系中使用backref
并指定了secondary
的话,另外一张表关联的relationship
字段会使用相同的secondary
。对象
back_populates
则须要在两张表的relationship
中都写上相同的secondary
中间表。sqlalchemy
secondary
参数能够是一个可调用对象,作一些 trick 的时候应该有用。姑且记下。ip
class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) child = relationship("Child", back_populates="parent") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) parent = relationship("Parent", back_populates="child")
parent
包含多个child
的一对多关系。child
里写ForeignKey
为parent
的主键,child
里写relationship
,parent
里一样写relationship
,back_populates
填充上,完事。ci
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", back_populates="child")
一对一关系中parent
须要在relationship
里加入参数uselist
,其余相同,完事儿。io
多对多关系须要一个中间表。table
association_table = Table('association', Base.metadata, Column('left_id', Integer, ForeignKey('left.id')), Column('right_id', Integer, ForeignKey('right.id')) ) class Parent(Base): __tablename__ = 'left' id = Column(Integer, primary_key=True) children = relationship( "Child", secondary=association_table, back_populates="parents") class Child(Base): __tablename__ = 'right' id = Column(Integer, primary_key=True) parents = relationship( "Parent", secondary=association_table, back_populates="children")
中间表里写上parent
和child
的主键做为foreignkey
,parent
和child
里的relationship
加入参数secondary
,指定为中间表。class