我知道这是一个典型的问题,但我做错了什么?
设置后
FLASK_APP='safecanyons:app'
flask run
应用程序已启动,但没有创建任何数据表。以下是主要脚本:
from flask import Flask, request, jsonify, g
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager, jwt_required
from safecanyons.models.users import User
from safecanyons.v1.users import bp as bp_users
from safecanyons.settings.dev import SQLALCHEMY_DATABASE_URI
from psycopg2 import connect
from flask_sqlalchemy import SQLAlchemy
from safecanyons.models.users import User
def create_app():
# The imports :
# from safecanyons.settings.dev import SQLALCHEMY_DATABASE_URI
# SQLALCHEMY_DATABASE_URI = "postgresql://{}:{}@{}/{}".format(
# DB_USER, DB_PASSWORD, DB_HOST,DB_NAME
# )
#
# from safecanyons.models.users import User
#
# from flask_sqlalchemy import SQLAlchemy
# db = SQLAlchemy()
# class User(db.Model):
# __tablename__ = 'users'
# id = db.Column(db.Integer, primary_key=True)
# username = db.Column(db.String(80), unique=True, nullable=False)
# email = db.Column(db.String(120), unique=True, nullable=False)
# def __repr__(self):
# return '<User %r>' % self.username
uri = SQLALCHEMY_DATABASE_URI
app = Flask('safecanyons')
app.config['SQLALCHEMY_DATABASE_URI'] = uri
SQLALCHEMY_TRACK_MODIFICATIONS = True
db = SQLAlchemy(app)
from safecanyons.models.users import User
db.create_all()
jwt = JWTManager(app)
app.register_blueprint(bp_users,url_prefix='/v1/users')
return app,db
app,db = create_app()
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
我尝试使用app.app_context()
,但它不像我预期的那样工作。当我到数据库查看用户表创建时,没有任何表存在。
db.create_all()
时呢?你必须明确地执行它。 - jorzel