如何使用flask-sqlalchemy 进行多对多查询
1个回答
展开全部
示例
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
sc = db.Table('sc',
db.Column('sno', db.String(10), db.ForeignKey('students.sno')),
db.Column('cno', db.String(10), db.ForeignKey('courses.cno'))
)
class Students(db.Model):
__tablename__ = 'students'
sno = db.Column(db.String(10), primary_key=True)
sname = db.Column(db.String(10))
class Course(db.Model):
__tablename__ = 'courses'
cno = db.Column(db.String(10), primary_key=True)
cname = db.Column(db.String(10), index=True)
students = db.relationship('Student',
secondary=sc,
backref=db.backref('courses',lazy='dynamic'),
lazy='dynamic'
)
#查询
student = Student.query.filter_by(sname=u'张三')
courses = student.courses.all() #该学生选择的所有课程
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询