from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
import pymysql
pymysql.install_as_MySQLdb()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']="mysql://%s:%s@%s/%s"%('root', '123456', '127.0.0.1', 'flask_book')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = 'lvmanba'
app.config.update(DEBUG=True)
db = SQLAlchemy(app)
@app.route('/',methods=['POST','GET'])
def index():
return 'hello'
class Author(db.Model):
__tablename__ = 'authors'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(16),unique=True)
books = db.relationship('Book',backref='author')
class Book(db.Model):
__tablename__ = 'books'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(16),unique=True)
author_id = db.Column(db.Integer, db.ForeignKey('authors.id'))
#创建表
try:
db.drop_all()
db.create_all()
except Exception as e:
print(e)
if __name__ == '__main__':
app.run(debug=True)
原本创建两个表,但是每次只创建第一个,加入了其他的类也是这种问题,前面的都可以,最后一个不会创建,而且不报错。应该是网站编辑器的问题,里面的下划线显示不出来。