from flask import Flaskfrom flask_sqlalchemy import sqlAlchemyapp = Flask(__name__)db = sqlAlchemy(app)app.config['sqlALCHEMY_DATABASE_URI'] = 'MysqL+pyMysqL://root:[email protected]/flask'一对多
class Singer(db.Model): ID = db.Column(db.Integer,primary_key=True) name = db.Column(db.String(70),unique=True) songs = db.relationship('Song',backref='singer')class Song(db.Model): ID = db.Column(db.Integer,unique=True) singer_ID = db.Column(db.Integer,db.ForeignKey('singer.ID'))多对一(不是必须时还是用与一对多吧)
比一对多的用法,会少一个从一里访问多的字段。
class CitiZen(db.Model): ID = db.Column(db.Integer,unique=True) city_ID = db.Column(db.Integer,db.ForeignKey('city.ID')) city = db.relationship('City')class Country(db.Model): ID = db.Column(db.Integer,primary_key=True) name = db.Column(db.String(79),unique=True) cAPItal = db.relationship('CAPItal',useList=False)一对一
class Country(db.Model): ID = db.Column(db.Integer,useList=False)class CAPItal(db.Model): ID = db.Column(db.Integer,unique=True) country_ID = db.Column(db.Integer,db.ForeignKey('country.ID')) country = db.relationship('Country'多对多
association_table = db.table( 'association',db.Column('student_ID',db.Integer,db.ForeignKey('student.ID')),db.Column('teacher_ID',db.ForeignKey('teacher.ID')))class Student(db.Model): ID = db.Column(db.Integer,unique=True) grade = db.Column(db.String(20)) teachers = db.relationship( 'Teacher',secondary=association_table,backref='students')class Teacher(db.Model): ID = db.Column(db.Integer,unique=True) office = db.Column(db.String(20))总结
以上是内存溢出为你收集整理的Flask数据库关系全部内容,希望文章能够帮你解决Flask数据库关系所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)