简体   繁体   English

Flask-sqlalchemy 连接到现有数据库

[英]Flask-sqlalchemy connecting to an existing database

I am trying to connect to an existing database and I am unsure where to put db.Model.metadata.reflect(db.engine) as shown in the following question .我正在尝试连接到现有数据库,但不确定将db.Model.metadata.reflect(db.engine)放在哪里,如以下问题所示。

My flask app that is structured like:我的烧瓶应用程序的结构如下:

在此处输入图片说明

I am not sure where to add db.Model.metadata.reflect(db.engine) .我不确定在哪里添加db.Model.metadata.reflect(db.engine)

In intel\\__init__.py I am using a function to create app:intel\\__init__.py我使用一个函数来创建应用程序:

def create_app():
    app = Flask(__name__)
    app.config.from_object('config')
    register_extensions(app)
    register_blueprints(app)
    return app

And in my register_extensions function:在我的 register_extensions 函数中:

def register_extensions(app):
    db.init_app(app)
    db.Model.metadata.reflect(db.engine)
    return None

in my model\\sam.pymy model\\sam.py

from intel.database import db


class Sam(db.Model):
    __table__ = db.Model.metadata.tables['sam_monthly_extract']

Now, when I do that I get an error:现在,当我这样做时,我收到一个错误:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from intel import create_app
  File "C:\Users\spitf\G2XChange\intel_app\intel\__init__.py", line 6, in <module>
    from intel.views.sam_view import sam_blueprint
  File "C:\Users\spitf\G2XChange\intel_app\intel\views\sam_view.py", line 3, in <module>
    from intel.models.sam import Sam
  File "C:\Users\spitf\G2XChange\intel_app\intel\models\sam.py", line 4, in <module>
    class Sam(db.Model):
  File "C:\Users\spitf\G2XChange\intel_app\intel\models\sam.py", line 5, in Sam
    __table__ = db.Model.metadata.tables['sam_monthly_extract']
KeyError: 'sam_monthly_extract'

What am I doing wrong?我究竟做错了什么?

Edit:编辑:

run.py

from intel import create_app

app = create_app()
app.run(debug=True)

I came across your question in my search for an answer to the same exact problem and since it helped me actually get there, I had to make sure to come back and provide you with what helped solve my issue.我在寻找相同问题的答案时遇到了您的问题,并且由于它帮助我真正达到了目标,因此我必须确保回来为您提供帮助解决我的问题的方法。

What I think was happening was that the __init__.py that contained the application factory was imported from my run.py and so was the import of the models.py which was at the bottom of the __init__ file, which imported before I could run the function to create the app and not having any context from which to pull that table from.认为发生的事情是包含应用程序工厂的__init__.py是从我的 run.py 导入的,因此导入了位于__init__文件底部的 models.py,它在我可以运行之前导入函数来创建应用程序,但没有任何上下文可以从中提取该表。 That's at least the best I can explain it currently.这至少是我目前能解释的最好的。

What solved it for me was to move the models import into the application factory, explicitly under the app context, right after db.init_app() like so:为我解决的是将模型导入移动到应用程序工厂中,明确在应用程序上下文下,紧跟在 db.init_app() 之后,如下所示:

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)

    with app.app_context():
        db.Model.metadata.reflect(db.engine)
        from app import models

Not sure if that is the best way to do it, but it's been working for me ever since.不确定这是否是最好的方法,但从那以后它一直对我有用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM