翻译自:Relationship Configuration
基本关系模式
下面每一节需要导入如下模块:
1 | from sqlalchemy import Table, Column, Integer, ForeignKey |
一对多
1 | class Parent(Base): |
多对一
1 | class Parent(Base): |
一对一
1 | class Parent(Base): |
多对多
1 | association_table = Table('association', Base.metadata, |
关联对象
1 | class Association(Base): |
邻接列表关系
1 | class Node(Base): |
1 | root --+---> child1 |
1 | id parent_id data |
反向链接关系
1 | from sqlalchemy import Integer, ForeignKey, String, Column |