Basic Relationship Patterns

基本关系模式 
The imports used for each of the following sections is as follows: 
下列的 import 语句,应用到接下来所有的代章节中:

from sqlalchemy import Table, Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

One To Many

A one to many relationship places a foreign key on the child table referencing the parent. 
表示一对多的关系时,在子表类中通过 foreign key (外键)引用父表类。 
relationship() is then specified on the parent, as referencing a collection of items represented by the child: 
然后,在父表类中通过 relationship() 方法来引用子表的类:

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship("Child")
# 在父表类中通过 relationship() 方法来引用子表的类集合 class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
# 在子表类中通过 foreign key (外键)引用父表的参考字段

To establish a bidirectional relationship in one-to-many, where the “reverse” side is a many to one, 
一对多的关系中建立双向的关系,这样的话在对方看来这就是一个多对一的关系, 
specify an additional relationship() and connect the two using the relationship.back_populates parameter: 
在子表类中附加一个 relationship() 方法,并且在双方的 relationship() 方法中使用 relationship.back_populates 方法参数:

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = 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="children")
# 子表类中附加一个 relationship() 方法
# 并且在(父)子表类的 relationship() 方法中使用 relationship.back_populates 参数

Child will get a parent attribute with many-to-one semantics. 
这样的话子表将会在多对一的关系中获得父表的属性

Alternatively, the backref option may be used on a single relationship() instead of using back_populates: 
或者,可以在单一的 relationship() 方法中使用 backref 参数来代替 back_populates 参数:

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship("Child", backref="parent") class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))

One To One

One To One is essentially a bidirectional relationship with a scalar attribute on both sides. 
一对一是两张表之间本质上的双向关系。 
To achieve this, the uselist flag indicates the placement of a scalar attribute instead of a collection on the “many” side of the relationship. 
要做到这一点,只需要在一对多关系基础上的父表中使用 uselist 参数来表示。 
To convert one-to-many into one-to-one:

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")

To convert many-to-one into one-to-one:

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child", back_populates="parent") class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent = relationship("Parent", back_populates="child", uselist=False)

As always, the relationship.backref and backref() functions may be used in lieu of the relationship.back_populates approach; to specifyuselist on a backref, use the backref() function:

同样的,可以使用下面这种方式:

from sqlalchemy.orm import backref

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child", backref=backref("parent", uselist=False)) class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))

Many To Many

Many to Many adds an association table between two classes. 
多对多关系会在两个类之间增加一个关联的表。 
The association table is indicated by the secondary argument to relationship()
这个关联的表在 relationship() 方法中通过 secondary 参数来表示。 
Usually, the Table uses the MetaData object associated with the declarative base class, 
通常的,这个表会通过 MetaData 对象来与声明基类关联, 
so that the ForeignKey directives can locate the remote tables with which to link: 
所以这个 ForeignKey 指令会使用链接来定位到远程的表:

定义中间表可以定义中间关系表相关的类,也可以直接通过Base.metdata生成对应关系表对象,不过基于code first准则,还是推荐将中间关系写成类。

构建第三张关系类实现多对多。

Base = declarative_base() #生成sqlorm基础类

class HostUserToGroup(Base):
__tablename__ = 'hostuser_to_group' # 表名hostuser_to_group
nid = Column(Integer, primary_key=True,autoincrement=True)
hostuser_id = Column(Integer,ForeignKey('host_user.id'),primary_key=True)# 外键关联host_user表的id字段
group_id = Column(Integer,ForeignKey('group.id'),primary_key=True) # 外键关联group表的id字段 class UserProfileToGroup(Base):
__tablename__ = 'userprofile_to_group'# 表名userprofile_to_group
nid = Column(Integer, primary_key=True,autoincrement=True)
userprofile_id = Column(Integer,ForeignKey('user_profile.id'),primary_key=True)# 外键关联user_profile表的id字段
group_id = Column(Integer,ForeignKey('group.id'),primary_key=True)# 外键关联group表的id字段 class UserProfileToHostUser(Base):
__tablename__ = 'userprofile_to_hostuser'# 表名userprofile_to_hostuser
nid = Column(Integer, primary_key=True,autoincrement=True)
userprofile_id = Column(Integer,ForeignKey('user_profile.id'),primary_key=True)# 外键关联user_profile表的id字段
hostuser_id = Column(Integer,ForeignKey('host_user.id'),primary_key=True)# 外键关联host_user表的id字段 class Host(Base):
__tablename__ = 'host' #表名host
id = Column(Integer, primary_key= True, autoincrement= True)# id字段,主键,自动增长
hostname = Column(String(64),unique= True,nullable= False)# hostname字段,唯一,不能为空
ip_addr = Column(String(64),unique= True,nullable= False)#ip_addr字段,唯一,不能为空
port = Column(Integer, default = 22) # port字段,整形,默认22
def __repr__(self):
return "<Hostobject: id=%s, hostname=%s, ip_addr=%s, port=%s>" %(self.id, self.hostname, self.ip_addr, self.port) class Group(Base):
__tablename__ = 'group' # 表名group
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
name = Column(String(64), unique = True, nullable = False) # name字段,唯一,不为空
def __repr__(self):
return "<Group object: id=%s, name=%s>" %(self.id, self.name) class UserProfile(Base):
__tablename__ = 'user_profile' # 表名user_profile
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
username = Column(String(64), unique = True, nullable = False) # username字段,唯一,不为空
password = Column(String(255), nullable = False) # password字段,不为空
hostusers = relationship('HostUser', secondary = UserProfileToHostUser.__tablename__, backref = 'user_profiles') # 多对多关联HostUser表类(注意不是表名),中间表类UserProfileToHostUser(注意不是表名),反向字段为user_profiles
groups = relationship('Group', secondary = UserProfileToGroup.__tablename__, backref = 'user_profiles') # 多对多关联Group表类(注意不是表名),中间表类UserProfileToGroup(注意不是表名),反向字段为user_profiles def __repr__(self):
return "<UserProfile object: id=%s, username=%s>" %(self.id, self.username) class HostUser(Base):
__tablename__ = 'host_user' # 表名host_user
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
host_id = Column(Integer, ForeignKey('host.id')) # host_id,外键关联host表的id字段
AuthTypes = [
(u'ssh-password', u'SSH/Password'),
(u'ssh-key', u'SSH/Key'),
] # 选项列表
auth_type = Column(ChoiceType(AuthTypes)) # auth_type字段,只能是选项列表里规定的值
username = Column(String(64), nullable = True) # username字段,不为空
password = Column(String(255)) # password字段
host = relationship('Host', backref = 'host_users')
groups = relationship('Group', secondary = HostUserToGroup.__tablename__, backref = 'host_users') # 多对多关联Group表类(注意不是表名),中间表类HostUserToGroup(注意不是表名),反向字段为host_users
__table_args = (UniqueConstraint('host_id', 'username', name = '_host_username_uc')) # host_id和username组成联合唯一约束 def __repr__(self):
return "<HostUser object: id=%s, host_id=%s, username=%s>" %(self.id, self.host_id, self.username)

构建关系表实现多对多实例如下:

from sqlalchemy import create_engine,and_,or_,func,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,\
ForeignKey, UniqueConstraint, DateTime
from sqlalchemy.orm import sessionmaker,relationship
from sqlalchemy_utils import ChoiceType,PasswordType
from datetime import datetime Base = declarative_base() #生成sqlorm基础类 HostUserToGroup = Table('hostuser_to_group', Base.metadata, # 表名hostuser_to_group
Column('hostuser_id', ForeignKey('host_user.id'), primary_key = True), # 外键关联host_user表的id字段
Column('group_id', ForeignKey('group.id'), primary_key = True), # 外键关联group表的id字段
) UserProfileToGroup = Table('userprofile_to_group', Base.metadata, # 表名userprofile_to_group
Column('userprofile_id', ForeignKey('user_profile.id'), primary_key = True), # 外键关联user_profile表的id字段
Column('group_id', ForeignKey('group.id'), primary_key = True), # 外键关联group表的id字段
) UserProfileToHostUser = Table('userprofile_to_hostuser', Base.metadata, # 表名userprofile_to_hostuser
Column('userprofile_id', ForeignKey('user_profile.id'), primary_key = True), # 外键关联user_profile表的id字段
Column('hostuser_id', ForeignKey('host_user.id'), primary_key = True), # 外键关联host_user表的id字段
) class Host(Base):
__tablename__ = 'host' #表名host
id = Column(Integer, primary_key= True, autoincrement= True)# id字段,主键,自动增长
hostname = Column(String(64),unique= True,nullable= False)# hostname字段,唯一,不能为空
ip_addr = Column(String(64),unique= True,nullable= False)#ip_addr字段,唯一,不能为空
port = Column(Integer, default = 22) # port字段,整形,默认22
def __repr__(self):
return "<Hostobject: id=%s, hostname=%s, ip_addr=%s, port=%s>" %(self.id, self.hostname, self.ip_addr, self.port) class Group(Base):
__tablename__ = 'group' # 表名group
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
name = Column(String(64), unique = True, nullable = False) # name字段,唯一,不为空
def __repr__(self):
return "<Group object: id=%s, name=%s>" %(self.id, self.name) class UserProfile(Base):
__tablename__ = 'user_profile' # 表名user_profile
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
username = Column(String(64), unique = True, nullable = False) # username字段,唯一,不为空
password = Column(String(255), nullable = False) # password字段,不为空
hostusers = relationship('HostUser', secondary = UserProfileToHostUser, backref = 'user_profiles') # 多对多关联HostUser表类(注意不是表名),中间表类UserProfileToHostUser(注意不是表名),反向字段为user_profiles
groups = relationship('Group', secondary = UserProfileToGroup, backref = 'user_profiles') # 多对多关联Group表类(注意不是表名),中间表类UserProfileToGroup(注意不是表名),反向字段为user_profiles def __repr__(self):
return "<UserProfile object: id=%s, username=%s>" %(self.id, self.username) class HostUser(Base):
__tablename__ = 'host_user' # 表名host_user
id = Column(Integer, primary_key = True) # id字段,主键,自动增长
host_id = Column(Integer, ForeignKey('host.id')) # host_id,外键关联host表的id字段
AuthTypes = [
(u'ssh-password', u'SSH/Password'),
(u'ssh-key', u'SSH/Key'),
] # 选项列表
auth_type = Column(ChoiceType(AuthTypes)) # auth_type字段,只能是选项列表里规定的值
username = Column(String(64), nullable = True) # username字段,不为空
password = Column(String(255)) # password字段
host = relationship('Host', backref = 'host_users')
groups = relationship('Group', secondary = HostUserToGroup, backref = 'host_users') # 多对多关联Group表类(注意不是表名),中间表类HostUserToGroup(注意不是表名),反向字段为host_users
__table_args = (UniqueConstraint('host_id', 'username', name = '_host_username_uc')) # host_id和username组成联合唯一约束 def __repr__(self):
return "<HostUser object: id=%s, host_id=%s, username=%s>" %(self.id, self.host_id, self.username)

Linking Relationships with Backref

简单来说, relationship函数是sqlalchemy对关系之间提供的一种便利的调用方式, backref参数则对关系提供反向引用的声明。

The backref keyword argument was first introduced in Object Relational Tutorial, and has been mentioned through- out many of the examples here. What does it actually do ? Let’s start with the canonical User and Address scenario:

from sqlalchemy import Integer, ForeignKey, String, Column from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True) name = Column(String)
addresses = relationship("Address", backref="user")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))

The above configuration establishes a collection of Address objects on User called User.addresses. It also establishes a .user attribute on Address which will refer to the parent User object.

In fact, the backref keyword is only a common shortcut for placing a second relationship() onto the Address mapping, including the establishment of an event listener on both sides which will mirror attribute op- erations in both directions. The above configuration is equivalent to:

from sqlalchemy import Integer, ForeignKey, String, Column 
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True) name = Column(String)
addresses = relationship("Address", back_populates="user")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship("User", back_populates="addresses")

Above, we add a .user relationship to Address explicitly. On both relationships, the back_populates directive tells each relationship about the other one, indicating that they should establish “bidirectional” behavior between each other. The primary effect of this configuration is that the relationship adds event handlers to both attributes which have the behavior of “when an append or set event occurs here, set ourselves onto the incoming attribute using this particular attribute name”. The behavior is illustrated as follows. Start with a User and an Address instance. The .addresses collection is empty, and the .user attribute is None:

下面来看下backref相关的源码

def backref(name, **kwargs):
"""Create a back reference with explicit keyword arguments, which are the same arguments one can send to :func:`relationship`. Used with the ``backref`` keyword argument to :func:`relationship` in
place of a string argument, e.g.:: 'items':relationship(
SomeItem, backref=backref('parent', lazy='subquery')) .. seealso:: :ref:`relationships_backref` """ return (name, kwargs)
param backref:
indicates the string name of a property to be placed on the related
mapper's class that will handle this relationship in the other
direction. The other property will be created automatically
when the mappers are configured. Can also be passed as a
:func:`.backref` object to control the configuration of the
new relationship.

SQLAlchemy_定义(一对一/一对多/多对多)关系的更多相关文章

  1. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  2. SSAS中事实表中的数据如果因为一对多或多对多关系复制了多份,在维度上聚合的时候还是只算一份

    SSAS事实表中的数据,有时候会因为一对多或多对多关系发生复制变成多份,如下图所示: 图1 我们可以从上面图片中看到,在这个例子中,有三个事实表Fact_People_Money(此表用字段Money ...

  3. JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析

    JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...

  4. JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用

    下面把项目中的用户类中有个:一对一  一对多  多对多的注解对应关系列取出来用于学习      说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...

  5. mybatis 一对一 一对多 多对多

    一对一 一对多 多对多

  6. day 69-70 一对一 一对多 多对一联表查询

    day 69 orm操作之表关系,多对多,多对一 多对一/一对多, 多对多{类中的定义方法} day69 1. 昨日内容回顾 1. 单表增删改查 2. 单表查询API 返回QuerySet对象的: 1 ...

  7. 使用NHibernate(7)-- 一对一 && 一对多 && 多对多

    1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...

  8. JPA 一对一 一对多 多对一 多对多配置

    1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  9. hibernate中一对多多对一关系设计的理解

    1.单向多对一和双向多对一的区别? 只需要从一方获取另一方的数据时 就使用单向关联双方都需要获取对方数据时 就使用双向关系 部门--人员 使用人员时如果只需要获取对应部门信息(user.getdept ...

随机推荐

  1. 编写可维护的JavaScript----笔记(一)

    1.缩进层级 建议使用4个空格为一个缩进层级,避免使用制表符进行缩进,可以通过配置文本编辑器来改变 缩进层级表示的内容. 2.语句末尾 有赖于分析器的自动分号插入机制(ASI),JavaScript可 ...

  2. 将字符“12345”转换成long型

    将字符“12345”转换成long型 解答: String s=”12345″; long num=Long.valueOf(s).longValue();

  3. Java反射基础(二)

    获取域   1. 通过反射API可以获取到类中公开的静态域和对象中的实例域.得到表示域的java.lang.reflect.Field类的对象之后,就可以获取和设置域的值. 与获取构造方法类似,Cla ...

  4. 关于在ubuntu平台下使用apt-get命令下载速度太慢的问题解决

    1. 进入设置,从哪进都一样找到就行. 2.选择软件与更新(Software and updates,英语估计是这个把) 点击下载自:这个下拉框,选中其它站点,选择镜像 选择阿里的或者搜狐的镜像,然后 ...

  5. iOS App 审核被拒的原因搜罗

    本文转载至 http://ju.outofmemory.cn/entry/108500   iOS app 审核 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游 ...

  6. Appcompat实现Action Bar的兼容性处理

    Appcompat实现Action Bar时,如果使用到split action bar或者Navigating Up with the App Icon需要考虑兼容性.下面介绍下split acti ...

  7. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...

  8. JZOJ.5230【NOIP2017模拟8.5】队伍统计

    Description 现在有n个人要排成一列,编号为1->n .但由于一些不明原因的关系,人与人之间可能存在一些矛盾关系,具体有m条矛盾关系(u,v),表示编号为u的人想要排在编号为v的人前面 ...

  9. JAVA环境变量配置备忘

    jdk1.6以上就不需要配置classpath了:系统会自动帮你配置好 选择“高级”选项卡,点击“环境变量”:在“系统变量”中,设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无 ...

  10. sql 循环表中记录

    =========================================================================循环排序查询数据=================== ...