SQLAlchemy 连接数据库及构建表格

举报
肥学 发表于 2022/03/29 01:44:41 2022/03/29
【摘要】 SQLAlchemy发音|ˈsiːkwəl’ælkəmi| 话不多说直接上代码 from flask_sqlalchemy import SQLAlchemy from flask import Fla...

SQLAlchemy发音|ˈsiːkwəl’ælkəmi|
话不多说直接上代码

from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from datetime import datetime
import pymysql
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"]="mysql+pymysql://root:root@localhost:3306/project_traning2"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["secret_key"]="hhh"
db = SQLAlchemy(app)


class Teacher(db.Model):
    __tablename__ = 'teacher'
    id = db.Column(db.Integer,primary_key=True,unique=True)
    num = db.Column(db.Integer,nullable=False,unique=True)

    name = db.Column(db.String(4),nullable=False)
    college = db.Column(db.String(30),nullable=False)
    password = db.Column(db.String(30),nullable=False)
    schedule=db.relationship('Schedule', backref=db.backref('teahcers',cascade='all, delete'))
    is_root = db.Column(db.Boolean ,default=False ,nullable=True)


class Student(db.Model):
    __tablename__ = 'student'
    id = db.Column(db.Integer, primary_key=True, unique=True)
    num = db.Column(db.Integer,nullable=False,unique=True)
    name = db.Column(db.String(4), nullable=False)
    major= db.Column(db.String(20), nullable=False)
    grade = db.Column(db.String(20),nullable=False)

    college = db.Column(db.String(30), nullable=False)
    password = db.Column(db.String(30), nullable=False)

    # class_point = db.relationship('Class_known', backref=db.backref('students'))
    proficiency = db.relationship('Proficiency', backref=db.backref('students',cascade='all, delete'))
    curricula= db.relationship('Curricula', backref=db.backref('students',cascade='all, delete'))
    tmp_curricula= db.relationship('Tmp_Curricula', backref=db.backref('students',cascade='all, delete'))




# 知识点
class Class_known(db.Model):
    __tablename__ = 'class_know'
    id = db.Column(db.Integer, primary_key=True, unique=True,nullable=False)
    name = db.Column(db.String(100), nullable=False)
    schedule_id = db.Column(db.Integer, db.ForeignKey('schedule.id', ondelete='CASCADE'))
    proficiency = db.relationship('Proficiency', backref=db.backref('class_kown',cascade='all, delete' ))
    # student_id = db.Column(db.Integer, db.ForeignKey('student.id'))
# 课程表
class Schedule(db.Model):
    __tablename__ = 'schedule'
    id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
    name = db.Column(db.String(30), nullable=False)
    major = db.Column(db.String(30), nullable=False)
    classes =db.Column(db.String(30), nullable=False)
    college = db.Column(db.String(30), nullable=False)
    teacher_id = db.Column(db.Integer, db.ForeignKey('teacher.id', ondelete='CASCADE'))
    grade = db.Column(db.String(30), nullable=False)
    random_code=db.Column(db.String(30), nullable=False)
    trem = db.Column(db.Date, index=True, default=datetime.utcnow())
    class_point=db.relationship('Class_known', backref=db.backref('schedules',cascade='all, delete'))
    curricula=db.relationship('Curricula', backref=db.backref('schedules',cascade='all, delete' ))
    tmp_curricula=db.relationship('Tmp_Curricula', backref=db.backref('schedules',cascade='all, delete'))

    # 熟练程度表




class Proficiency(db.Model):
    __tablename__ = 'proficiency'
    id = db.Column(db.Integer, primary_key=True, unique=True, nullable=False ,autoincrement=True)
    # 掌握情况
    proficiency = db.Column(db.String(50), nullable=True)
    # 评价
    estimate = db.Column(db.String(50), nullable=True)
    class_know_id = db.Column(db.Integer, db.ForeignKey('class_know.id', ondelete='CASCADE'))
    student_id = db.Column(db.Integer, db.ForeignKey('student.id', ondelete='CASCADE'))



class Curricula(db.Model):
    __tablename__ = 'curricula_variable'
    id = db.Column(db.Integer, primary_key=True, unique=True,autoincrement=True)
    schedule_id = db.Column(db.Integer, db.ForeignKey('schedule.id', ondelete='CASCADE'))
    student_id = db.Column(db.Integer, db.ForeignKey('student.id', ondelete='CASCADE'))



class Tmp_Curricula(db.Model):
    __tablename__ = 'tmp_curricula'
    id = db.Column(db.Integer, primary_key=True, unique=True,autoincrement=True)
    schedule_id = db.Column(db.Integer, db.ForeignKey('schedule.id', ondelete='CASCADE'))
    student_id = db.Column(db.Integer, db.ForeignKey('student.id', ondelete='CASCADE'))

# 超级管理员
class Surper_root(db.Model):
    __tablename__  ='root'
    id = db.Column(db.Integer,primary_key=True,unique=True,autoincrement=True)
    num =db.Column(db.Integer,nullable=False)
    name =db.Column(db.String(4),nullable=False)
    password =db.Column(db.String(25),nullable=False)

if __name__ == "__main__":
     db.create_all()

  
 
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

个人理解是SQLAlchemy就是一个在python里面操作数据库的一个东西管这种东西叫映射。另一种方法可以看这个

文章来源: blog.csdn.net,作者:肥学,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jiahuiandxuehui/article/details/110188585

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。