最佳答案
我试图将 Flask-SQLAlchemy 模型分离到单独的文件中,当我试图运行 db.create_all()
时,我得到 No application found. Either work inside a view function or push an application context.
返回文章页面
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
返回文章页面
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from shared.db import db
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'My connection string'
db.init_app(app)
返回文章页面
from shared.db import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email_address = db.Column(db.String(300), unique=True, nullable=False)
password = db.Column(db.Text, nullable=False)