sqlalchemy 各种关系的写法

原创
2019/03/01 16:10
阅读数 448

Intro

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

注意事项

ForeignKey

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

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

back_populates是更推荐的写法。

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

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

可调用的 secondary

secondary参数可以是一个可调用对象,做一些 trick 的时候应该有用。姑且记下。

一对多关系

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填充上,完事。

一对一关系

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,其他相同,完事儿。

多对多关系

多对多关系需要一个中间表。

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,指定为中间表。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部