One-to-many and many-to-many in Flask

1.ForeignKey

class Students(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    s_name = db.Column(db.String(20), unique=False, nullable=False)
    s_age = db.Column(db.Integer, default=18)
    s_g = db.Column(db.Integer, db.ForeignKey('grade.id'), nullable=True)
    
class Grade(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    g_name = db.Column(db.String(30), unique=True, nullable=False)
    students = db.relationship('Students', backref='grade')
    __tablename__ = 'grade'

One-to-many in flsk

On the more side, you need to write a field s_g and write db.ForeignKey('grade.id') in it. The parameter is the primary key of the one-to-many one. This field does not exist in the database.

On the lesser side, you need to add a relationship field students = db.relationship('Students', backref='grade'),

backref is the keyword for reverse query

Inquire:

Forward query (one check multiple)

stus = Grade.query.get(id).students

Reverse query (check one more)

the_grade = Student.query.get(id).grade

2.ManyToMany

class Students(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    s_name = db.Column(db.String(20), unique=False, nullable=False)
    s_age = db.Column(db.Integer, default=18)
    s_g = db.Column(db.Integer, db.ForeignKey('grade.id'), nullable=True)
    
class Course(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    c_name = db.Column(db.String(10), unique=True, nullable=False)
    students = db.relationship('Students', secondary='s_c', backref='cou')
    __tablename__ = 'course'

Intermediate table:

s_c = db.Table('s_c',
               db.Column('s_id', db.Integer, db.ForeignKey('students.id'), primary_key=True),
               db.Column('c_id', db.Integer, db.ForeignKey('course.id'), primary_key=True),
               )

Many-to-many can add a relationship field to any party. The first parameter is another table associated with it, the second parameter is the table name of the intermediate table, and the third parameter backref is the keyword for reverse query

The many-to-many intermediate table in flask needs to be created by itself. The class of the created intermediate table is db.Table(), the first parameter is the table name of the intermediate table, and then two fields are added to the table. These two fields are the one-to-many external of the two associated tables. The key is also the same as the primary key.

Inquire

Forward query (check the other side where the relationship is written)

stus = Course.query.get(id).students

Reverse lookup

course = Students.query.get(id).cou

Insert picture description here
Insert picture description here

Role model

class Role(db.Model):

__tablename__='role'
r_id=db.Column(db.Integer,autoincrement=True,primary_key=True)
r_name=db.Column(db.String(10))
user=db.relationship('User',backref='role')
 
#角色和权限的(多对多的)关联表
#r_p为关联表的表名
r_p=db.Table('r_p',
db.Column('role_id',db.Integer,db.ForeignKey('role.r_id'),primary_key=True),
db.Column('permission_id',db.Integer,db.ForeignKey('permission.p_id'),primary_key=True))

#权限模型表
class Permission(db.Model):
__tablename__='permission'
p_id=db.Column(db.Integer,autoincrement=True,primary_key=True)
p_name=db.Column(db.String(16),unique=True)
p_er=db.Column(db.String(16),unique=True)
#添加多对多的反向引用,必须使用secondary指定中间关联表
#用权限查询角色时用查询到的权限对象:“权限对象.roles.all()”得到其对应的所有角色
roles=db.relationship('Role',secondary=r_p,backref=db.backref('permission',lazy=True))
#db.backref('permission', 中的permission用来反向关联,用角色查询其对应的所有权限。用查询到的 '角色对象.permission.all()'得到。
###relationship可以放到任意一个类中都行,与之相反。###

#多对多关系查询

#根据角色找权限
####多对多关系中获取对象,只能用get(id)方法,不能通过filter或者filter_by来获取###
role=Role.query.get(id)
per=role.permission.all()
return ','.join(c.name for c in per)

#根据权限来找角色
per=Permission.query.get(id)
role=per.roles.all()
return ','.join(i.name for i in role)

#多对多关系添加
role=Role.query.get(id)
per=Permission.query.get(id)
#给角色添加权限
role.permission.append(per)

#多对多关系删除
role=Role.query.get(id)
per=Permission.query.get(id)
#给角色删除权限
role.permission.remove(per)

Summary: During ORM operation, the role permission table (intermediate association table) of the many-to-many relationship does not require user maintenance.

Guess you like

Origin blog.csdn.net/BigData_Mining/article/details/107962445