gpt4 book ai didi

python-3.x - Flask 迁移失败

转载 作者:行者123 更新时间:2023-12-05 09:12:29 26 4
gpt4 key购买 nike

不出所料,我正在使用 Flask-Migrate 在 Flask 中进行迁移。一旦我执行 python manage.py db init 它会创建目录 migrations 和初始迁移文件。然后我执行 python manage.py db migrate 并得到这个:

...
...
target_metadata = current_app.extensions['migrate'].db.metadata
AttributeError: 'NoneType' object has no attribute 'metadata'

我从这个输出中了解到“迁移”是 None 因此我收到了一个属性错误。

模型.py:

from sqlalchemy.sql import func
from project import db, bcrypt


class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(128), nullable=False, unique=True)
email = db.Column(db.String(128), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False)
active = db.Column(db.Boolean(), default=True, nullable=False)
created_date = db.Column(db.DateTime, default=func.now(), nullable=False)

def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = bcrypt.generate_password_hash(password).decode()

def to_json(self):
return {
'id': self.id,
'username': self.username,
'email': self.email,
'active': self.active,
}

问题是为什么没有传递给它?我正在学习教程,我不应该出现此错误。

我从类似的主题中得到了这个:

NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result.

这是我在 migrations 目录的 env.py 文件中找到的:

from flask import current_app
config.set_main_option('sqlalchemy.url',
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

current_app 是从 Flask 导入的,但不包含我需要从中使用元数据的扩展 migrate

虽然它没有理由抛出 None,因为扩展在 __init__.py 文件中正确初始化:

...
...
from flask_migrate import Migrate

db = SQLAlchemy()
toolbar = DebugToolbarExtension()
cors = CORS()
migrate = Migrate()
bcrypt = Bcrypt()

def create_app(script_info=None):
app = Flask(__name__)
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)
app.config.from_object('project.config.DevelopmentConfig')
toolbar.init_app(app)
cors.init_app(app)
db.init_app(app)
migrate.init_app(app) # <--
bcrypt.init_app(app)

from project.api.users import users_blueprint
app.register_blueprint(users_blueprint)

@app.shell_context_processor
def ctx():
return {'app': app, 'db': db}

return app

最佳答案

我在 migrate 扩展的初始化中缺少一个参数。 Migrate 接受 app 实例 db 实例。

def create_app(script_info=None):
app = Flask(__name__)
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)
app.config.from_object('project.config.DevelopmentConfig')
toolbar.init_app(app)
cors.init_app(app)
db.init_app(app)
migrate.init_app(app, db) # <--
bcrypt.init_app(app)

from project.api.users import users_blueprint
app.register_blueprint(users_blueprint)

@app.shell_context_processor
def ctx():
return {'app': app, 'db': db}

return app

关于python-3.x - Flask 迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com