'application not registered on db instance' 用户 table 截断后
'application not registered on db instance' after user table truncation
我在测试期间删除了用户 table 中的所有信息。现在,当我尝试 运行 一个 manage.py
命令来重新创建管理员用户时,我得到 RuntimeError: application not registered on db instance and no application bound to current context
。我一直在阅读有关 Flask 上下文的信息,尤其是 What is the purpose of Flask's context stacks?,但我不清楚如何解决这个问题。我该如何解决这个错误?
$ python manage.py db create_admin
Traceback (most recent call last):
File "manage.py", line 58, in <module>
manager.add_command("create_admin", create_admin())
File "manage.py", line 47, in create_admin
confirmed_on=datetime.datetime.now()
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\orm\scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\util\_collections.py", line 878, in __call__
return self.registry.setdefault(key, self.createfunc())
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 704, in create_session
return SignallingSession(self, **options)
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 149, in __init__
self.app = db.get_app()
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 845, in get_app
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
manage.py
:
import os
from flask_script import Manager
from myflaskapp.app import create_app
from myflaskapp.models.user import User
from myflaskapp.database import db
manager = Manager(app)
@manager.command
def create_admin():
db.session.add(User(
username="admin1",
email="ad@min.com",
password="admin",
admin=True,
confirmed=True,
confirmed_on=datetime.datetime.now()
))
db.session.commit()
manager.add_command("create_admin", create_admin())
manager.run()
您正在使用 @manager.command
,因此您不应同时使用 manager.add_command
。完全删除行 manager.add_command('create_admin', create_admin())
.
如果您想使用 add_command
,请删除 @manager.command
并使用 manager.add_command('create_admin', Command(create_admin))
。
您收到错误是因为应用程序上下文仅在管理器 运行 时处于活动状态,但您过早地调用了该函数。
我在测试期间删除了用户 table 中的所有信息。现在,当我尝试 运行 一个 manage.py
命令来重新创建管理员用户时,我得到 RuntimeError: application not registered on db instance and no application bound to current context
。我一直在阅读有关 Flask 上下文的信息,尤其是 What is the purpose of Flask's context stacks?,但我不清楚如何解决这个问题。我该如何解决这个错误?
$ python manage.py db create_admin
Traceback (most recent call last):
File "manage.py", line 58, in <module>
manager.add_command("create_admin", create_admin())
File "manage.py", line 47, in create_admin
confirmed_on=datetime.datetime.now()
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\orm\scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\util\_collections.py", line 878, in __call__
return self.registry.setdefault(key, self.createfunc())
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 704, in create_session
return SignallingSession(self, **options)
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 149, in __init__
self.app = db.get_app()
File "c:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py", line 845, in get_app
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
manage.py
:
import os
from flask_script import Manager
from myflaskapp.app import create_app
from myflaskapp.models.user import User
from myflaskapp.database import db
manager = Manager(app)
@manager.command
def create_admin():
db.session.add(User(
username="admin1",
email="ad@min.com",
password="admin",
admin=True,
confirmed=True,
confirmed_on=datetime.datetime.now()
))
db.session.commit()
manager.add_command("create_admin", create_admin())
manager.run()
您正在使用 @manager.command
,因此您不应同时使用 manager.add_command
。完全删除行 manager.add_command('create_admin', create_admin())
.
如果您想使用 add_command
,请删除 @manager.command
并使用 manager.add_command('create_admin', Command(create_admin))
。
您收到错误是因为应用程序上下文仅在管理器 运行 时处于活动状态,但您过早地调用了该函数。