config.py文件
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:root@localhost/myDb'
SQLALCHEMY_TRACK_MODIFICATIONS = False
模型文件
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import Blueprint
import sys
sys.path.append('..')
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
class users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(36), nullable=False)
def __init__(self, name, email, password):
self.name = name
users = Blueprint('users', __name__)
@users.route('/createAll', methods=['POST'])
def create():
db.create_all()
return "success"
主文件
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from Models.Models import users
from flask import Blueprint
from config import Config
app = Flask(__name__)
db = SQLAlchemy(app)
app.register_blueprint(users)
if __name__ == '__main__':
app.run()
我希望db.create_all()运行并创建我的模型。我最近正在试验蓝图,并且此代码在没有蓝图的情况下仍能正常工作,就像在文件中一样。不知道我在做什么错。如果我继续执行邮递员并执行/ createAll POST请求,它将像db.create_all()中那样使我“成功”,因为没有建立我的表,所以它什么也没做。
我的文件夹的顺序是
(flask-practise - folder)
(Models - folder)
Models.py
app.py
config.py