简体   繁体   English

Flask和SQLAlchemy,应用程序未在实例上注册

[英]Flask and SQLAlchemy, application not registered on instance

I'm currently trying to piece together a small Flask application. 我正在尝试拼凑一个小型Flask应用程序。 This is my structure. 这是我的结构。

run.py
application
  __init__.py
  database.py
  models.py
  views.py

database.py contains just the SQLAlchemy object: database.py只包含SQLAlchemy对象:

db = SQLAlchemy()

I then import this into my models.py to create my models. 然后我将其导入到models.py以创建我的模型。 Lastly, inside __init__.py I import db from database.py and do: 最后,在__init__.py我从database.py导入db并执行:

from .database import db
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///application.db'
db.init_app(app)
db.create_all()

However, I cannot create the tables from the models it appears. 但是,我无法从它出现的模型中创建表。 If I remove db.create_all() . 如果我删除db.create_all() The application will run with no problems, but obviously the database is not created. 应用程序将运行没有问题,但显然没有创建数据库。 When db.create_all() is present I am given 'RuntimeError: application not registered on db instance and no application bound to current context'. db.create_all()存在时,我得到'RuntimeError:应用程序未在db实例上注册,并且没有应用程序绑定到当前上下文'。

I'm honestly confused, as before I was having issues just starting the application without creating the database, but moving db to it's own file seems to somehow have corrected that issue. 老实说我很困惑,就像之前我在没有创建数据库的情况下启动应用程序时出现问题一样,但将db移动到它自己的文件似乎已经在某种程度上纠正了这个问题。 Now, the only issue remains is actually creating the database. 现在,唯一的问题仍然是创建数据库。

Can anyone tell me what might be the problem? 谁能告诉我可能是什么问题? I'm genuinely stumped. 我真的很难过。

The answer is here: http://flask-sqlalchemy.pocoo.org/latest/api/#configuration 答案在这里: http//flask-sqlalchemy.pocoo.org/latest/api/#configuration

See the part about: 请参阅以下部分:

The difference between the two is that in the first case methods like create_all() and drop_all() will work all the time but in the second case a flask.Flask.request_context() has to exist. 两者之间的区别在于,在第一种情况下,create_all()和drop_all()等方法将一直有效,但在第二种情况下,必须存在flask.Flask.request_context()。

There's more information here: http://flask-sqlalchemy.pocoo.org/latest/contexts/ 这里有更多信息: http//flask-sqlalchemy.pocoo.org/latest/contexts/

If all that is confusing (it probably is, since it's talking about a fairly advanced feature of Flask), the short short version is db.init_app(app) changes the app object, but it doesn't change anything in the db object. 如果所有这一切都令人困惑(可能是因为它正在讨论Flask的一个相当高级的功能),那么简短的短版本是db.init_app(app)更改app对象,但它不会改变db对象中的任何内容。 It's on purpose because there might be more than one app flying around, and db might have to talk to all of them. 这是有目的的,因为可能有不止一个app飞来飞去, db可能不得不与所有人交谈。 (I said it was an advanced feature.) (我说这是一个高级功能。)

So when you call db.create_all() without having a request live (which creates a global that has the currently running app ) it doesn't know what to connect to, and bombs. 因此,当您在没有实时请求(创建具有当前正在运行的app的全局db.create_all()情况下调用db.create_all() ,它不知道要连接到什么,以及炸弹。 That's what the error means. 这就是错误的含义。

In your case, I would put the SQLAlchemy call back in __init__.py and pass app to it, that's the easiest way: 在你的情况下,我会将SQLAlchemy调用回__init__.py并将app传递给它,这是最简单的方法:

db = SQLAlchemy(app)

Or keep things as they are, and run the setup before the first request: 或保持原样,并在第一次请求之前运行设置:

@app.before_first_request
def create_database():
     db.create_all()

I hope that helps! 我希望有所帮助! Let me know if you run into any more problems. 如果您遇到更多问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask-SQLAlchemy,“应用程序未在数据库上注册” - Flask-SQLAlchemy, “Application not registered on db” 使用Flask-SQLAlchemy反映表会引发RuntimeError:应用程序未注册 - Reflecting tables with Flask-SQLAlchemy raises RuntimeError: application not registered Flask:应用程序未在数据库实例上注册并且没有应用程序绑定到当前上下文 - Flask :Application not registered on db instance and no application bound to current context 烧瓶sqlalchemy-最小应用 - Flask sqlalchemy - Minimal Application 未找到烧瓶sqlalchemy的应用程序 - No application found with flask sqlalchemy 应用程序未在数据库实例上注册,并且没有应用程序 - application not registered on db instance and no application AssertionError:sqlalchemy 扩展未注册到当前应用程序 - AssertionError: The sqlalchemy extension was not registered to the current application 烧瓶。 应用程序上下文和访问db.session的新线程。 应用程序未在db实例上注册,也没有应用程序绑定到当前上下文 - Flask. App context and access to db.session a new thread. Application not registered on db instance and no application bound to current context 在哪里用Flask-restplus初始化SQLAlchemy实例 - Where to initialize SQLAlchemy instance with Flask-restplus 更新 flask Z21D6F40CFB511982E457ZA 中的 sqlalchemy orm 实例 - Updating sqlalchemy orm instance in flask session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM