from flask_sqlalchemy import SQLAlchemy 无法创建数据库表,代码无报错,代码如下,python版本是3.5
fromflaskimportFlaskfromflask_sqlalchemyimportSQLAlchemyapp=Flask(__name__)app.config...
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:666888@127.0.0.1/flask_demo'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# 角色(管理员/普通用户)
# 用户(角色id)
class Role(db.Model):
#定义表
__tablename__ = 'roles'
#定义字段
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'))
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
#删除表
db.drop_all()
# 创建表
db.create_all()
app.run() 展开
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:666888@127.0.0.1/flask_demo'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# 角色(管理员/普通用户)
# 用户(角色id)
class Role(db.Model):
#定义表
__tablename__ = 'roles'
#定义字段
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'))
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
#删除表
db.drop_all()
# 创建表
db.create_all()
app.run() 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询