sqlalchemy 各类关系的写法

Intro

由于 sqlalchemy 这玩意儿不是特别经常使用,偶然提起的时候想写个多对多关系还搜索了半天。因而趁机作个笔记。python

注意事项

ForeignKey

db.ForeginKey的参数是<表名>.<键名>,而不是<类名>.<字段名>,务必注意这个区别。sql

back_populates 和 backref 在多对多关系中使用的区别

back_populates是更推荐的写法。code

多对多关系中使用backref并指定了secondary的话,另外一张表关联的relationship字段会使用相同的secondary对象

back_populates则须要在两张表的relationship中都写上相同的secondary中间表。sqlalchemy

可调用的 secondary

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里写ForeignKeyparent的主键,child里写relationshipparent里一样写relationshipback_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")

中间表里写上parentchild的主键做为foreignkeyparentchild里的relationship加入参数secondary,指定为中间表。class

相关文章
相关标签/搜索