表关系
- 表之间的关系存在三种:
- 一对一、一对多、多对多。
- 而SQLAlchemy中的ORM也可以模拟这三种关系。因为一对一其实在SQLAlchemy中底层是通过一对多的方式模拟的,
- 所以先来看下一对多的关系
ForeignKey
- # -*- coding: utf-8 -*-
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy import Column # 列
- from sqlalchemy import Integer, String, ForeignKey # 属性
- from sqlalchemy import create_engine
- from sqlalchemy.orm import relationship #
- Base = declarative_base() # django models
- class Article(Base):
- __tablename__ = "article"
- id = Column(Integer, primary_key=True, autoincrement=True)
- title = Column(String(30), nullable=False)
- content = Column(String(30), nullable=False)
- uid = Column(Integer, ForeignKey('user.id'))
- def __repr__(self):
- return "<Article(title:%s)>" % self.title
- class Users(Base):
- __tablename__ = 'user'
- id = Column(Integer, primary_key=True, autoincrement=True)
- username = Column(String(50), nullable=False)
- engine = create_engine("mysql+pymysql://root:redhat@192.168.32.71:3306/my_sql?charset=utf8")
- # 去engine数据库中创建所有继承Base的表
- Base.metadata.create_all(engine)
- 外键约束有以下几项:
- RESTRICT:父表数据被删除,会阻止删除。默认就是这一项。
- NO ACTION:在MySQL中,同RESTRICT。
- CASCADE:级联删除。
- SET NULL:父表数据被删除,子表数据会设置为NULL。
一对多
- # 一对多
- class Address(Base):
- __tablename__ = 'address'
- id = Column(Integer, primary_key=True)
- email_address = Column(String(30), nullable=False)
- # FK
- # User表的外键,指定外键的时候,是使用的是数据库表的名称,而不是类名
- user_id = Column(Integer, ForeignKey('users.id'))
- # 在ORM层面绑定两者之间的关系,第一个参数是绑定的表的类名,
- # 第二个参数back_populates是通过User反向访问时的字段名称
- user = relationship('User', back_populates="addresses")
- def __repr__(self):
- return "<Address(email_address='%s')>" % self.email_address
- class User(Base):
- __tablename__ = 'users'
- id = Column(Integer, primary_key=True)
- name = Column(String(50))
- # 在ORM层面绑定和`Address`表的关系
- addresses = relationship("Address", order_by=Address.id, back_populates="user")
- # 注意:
- # 在User表中添加的addresses字段,可以通过User.addresses来访问和这个user相关的所有address
- # 在Address表中的user字段,可以通过Address.user来访问这个user。达到了双向绑定
例子
- Session = sessionmaker(engine)
- db_session = Session()
- peach = User(name='peach')
- peach.addresses = [Address(email_address='1234@qq.com'),
- Address(email_address='2345@qq.com')]
- db_session.add(peach)
- db_session.commit()
- db_session.close()
一对一
- # 一对一
- class Address(Base):
- __tablename__ = 'addresses'
- id = Column(Integer, primary_key=True)
- email_address = Column(String(50))
- user_id = Column(Integer, ForeignKey('users.id'))
- user = relationship('User', back_populates='addresses')
- class User(Base):
- __tablename__ = 'users'
- id = Column(Integer,primary_key=True)
- name = Column(String(50))
- # 设置uselist关键字参数为False
- addresses = relationship("Address", back_populates='user', uselist=False)
- # 只要在User表中的addresses字段上添加uselist=False就可以达到一对一的效果
- # 设置了一对一的效果后,就不能添加多个邮箱到user.addresses字段了
例子
- peach = User(name='peach')
- peach.addresses = Address(email_address='1234@qq.com')
多对多
- # 多对多
- from sqlalchemy import Table
- # 要创建一个多对多的关系表,首先需要一个中间表,通过Table来创建一个中间表
- association_table = Table('teacher_classes', # 中间表的表名
- Base.metadata, # Base的元类
- Column('teacher_id', Integer, ForeignKey('teacher.id')),
- Column('classes_id', Integer, ForeignKey('classes.id'))) # 要连接的两个表
- # Column 第一个参数是表示的是连接表的外键名
- # Column 第二个参数表示这个外键的类型
- # Column 第三个参数表示要外键的表名和字段
- class Teacher(Base):
- __tablename__ = 'teacher'
- id = Column(Integer,primary_key=True)
- name = Column(String(50))
- classes = relationship('Classes', secondary=association_table, back_populates='teachers') # 通过secondary参数来连接中间表
- class Classes(Base):
- __tablename__ = 'classes'
- id = Column(Integer,primary_key=True)
- name = Column(String(50))
- teachers = relationship('Teacher',secondary=association_table,back_populates='classes') # 通过secondary参数来连接中间表
例子
- Base.metadata.create_all(engine)
- Session = sessionmaker(engine)
- db_session = Session()
- teacher1 = Teacher(name='Teacher1')
- teacher2 = Teacher(name='teacher2')
- classes1 = Classes(name='classes1')
- classes2 = Classes(name='classes2')
- teacher1.classes = [classes1, classes2]
- classes1.teachers = [teacher1]
- db_session.add(teacher1)
- db_session.add(teacher2)
- db_session.add(classes1)
- db_session.add(classes2)
- db_session.close()