python3,Flask运用db.create_all()没辙建表题目

python3,Flask运用db.create_all()没辙建表题目

from flask import Flask

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

# 摆设数据库的地方

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:chenfy@127.0.0.1:3306/flask_sql_demo' #这边按照本人的数据库地方,数据库名字摆设

# 盯梢数据库的窜改,不倡导打开

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

'''

两张表(处置员/普遍用户)

用户(脚色ID)

'''

class Role(db.Model):

# 设置表名

__tablename__ = 'roles'

# 设置字段

# db.Column 表白是一个字段

id = db.Column(db.Integer, primary_key=True)

name = db.Column(db.String(16), unique=True)

class User(db.Model):

__tablename__ = 'users'

id = db.Column(db.Integer, primary_key=True)

name = db.Column(db.String(16), unique=True)

role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

db.drop_all()

db.create_all()

#要有依附包MySQLdb

@app.route('/')

def index():

return "hahah"

if __name__ == '__main__':

app.run()

分享到 :

Leave a Reply

Your email address will not be published. Required fields are marked *