欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

flask报错 AttributeError: 'NoneType' object has no attribute 'metadata'

程序员文章站 2022-03-26 19:33:22
...
  • flask数据库迁移时,报错:

(venv) D:\000flask>flask db migrate
Traceback (most recent call last):
	......
	File "migrations\env.py", line 27, in <module>
    target_metadata = current_app.extensions['migrate'].db.metadata
AttributeError: 'NoneType' object has no attribute 'metadata'
  • 代码如下:

db = SQLAlchemy()
migrate = Migrate()

def create_app(object_name):
	app = Flask(__name__)
	db.init_app(app)
    migrate.init_app(app)
  
    return app
  • 原因:

migrate对象初始化时db也应作为参数传入

migrate.init_app(app,db)