常见关系:
一对多关系(一个作者,多篇文章) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Author (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(70 ), unique=True ) phone = db.Column(db.String(20 )) articles = db.relationship('Article' ) class Article (db.Model): id = db.Column(db.Integer, primary_key=True ) title = db.Column(db.String(15 ), index=True ) body = db.Column(db.Text) author_id = db.Column(db.Integer, db.ForeignKey('author.id' )) >>>shansan = Author(name="shansan" ) >>>hello = Article(title="Hello world !" ) >>>boy = Article(title="Hello Boy !" ) >>>db.session.add(shansan) >>>db.session.add(hello) >>>db.session.add(boy) >>>shansan.articles.append(hello) >>>shansan.articles.append(boy) >>>db.session.commit()
基于一对多的双向关系(bidirectional relationship) 在这里我们希望可以在Book类中存在这样一个属性:通过调用它可以获取对应的作者的记录,这类返回单个值的关系属性称为标量关系属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Writer (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(64 ), unique=True ) books = db.relationship('Book' , back_populates='writer' ) def __repr__ (self ): return '<Writer %r>' % self.name class Book (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(50 ), index=True ) writer_id = db.Column(db.Integer, db.ForeignKey('writer.id' )) writer = db.relationship('Writer' , back_populates='books' ) def __repr__ (self ): return '<Book %r>' % self.name
多对一关系(多个市民都在同一个城市) 1 2 3 4 5 6 7 8 9 10 11 12 class Citizen (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(20 ), unique=True ) city_id = db.Column(db.Integer, db.ForeignKey('city.id' )) city = db.relationship('City' ) class City (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(20 ), unique=True )
一对一关系(国家和首都) 1 2 3 4 5 6 7 8 9 10 11 12 13 class Country (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(20 ), unique=True ) capital = db.relationship('Capital' , uselist=False ) class Capital (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(20 ), unique=True ) country_id= db.Column(db.Integer, db.ForeignKey('country.id' )) country = db.relationship('Country' )
多对多双向关系(老师和学生)
多对多关系的建立需要使用关联表(association table)。关联表不存储数据,只用来存储关系两侧模型的外键对应关系
定义关系两侧的关系函数时,需要添加一个secondary 参数,值设为关联表的名称
关联表由使用db.Table类定义,传入的第一个参数为关联表的名称
我们在关联表中将多对多的关系分化成了两个一对多的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 association_table = db.Table('association_table' , db.Column('student_id' , db.Integer, db.ForeignKey('teacher.id' )), db.Column('teacher_id' , db.Integer, db.ForeignKey('student.id' )) ) class Student (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(70 ), unique=True ) grade = db.Column(db.String(20 )) teachers = db.relationship('Teacher' , secondary=association_table,back_populates='students' ) class Teacher (db.Model): id = db.Column(db.Integer, primary_key=True ) name = db.Column(db.String(70 ), unique=True ) office = db.Column(db.String(20 )) students = db.relationship('Student' , secondary=association_table, back_populates='teachers' )
常用的SQLAlchemy关系函数参数和常用的SQLAlchemy关系记录加载方式(lazy参数可选值)
使用关系函数定义的属性不是数据库字段,而是类似于特定的查询函数
当关系属性被调用时,关系函数会加载相应的记录
相关 http://www.sqlalchemy.org/
https://github.com/sqlalchemy/sqlalchemy
https://github.com/mitsuhiko/flask-sqlalchemy