简体   繁体   English

Flask-SQLAlchemy多个数据库和绑定

[英]Flask-SQLAlchemy multiple databases and binds

URI: URI:

SQLALCHEMY_DATABASE_URI = "mssql+pymssql://user:password123@127.0.0.1/DbOne"


SQLALCHEMY_BINDS = {
    "sql_server": "mysql+pymysql://user:password123@127.0.0.1/DbTwo"
}

Models.py Models.py

class CrimMappings(db.Model):
    __tablename__ = "crim_mappings"
    id = db.Column(db.Integer, primary_key=True)
    date_mapped = db.Column(db.Date)
    county_id = db.Column(db.Integer)
    state = db.Column(db.String(20))
    county = db.Column(db.String(100))
    AgentID = db.Column(db.String(100), unique=True)
    CollectionID = db.Column(db.String(100))
    ViewID = db.Column(db.String(100))


class LicenseType(db.Model):
    __bind_key__ = "sql_server"
    __table__ = db.Model.metadata.tables["sbm_agents"]

however it throws me a KeyError saying the 'sbm_agents' table is not found which it should be there because I've specified the bind key to point to the sql_server bind. 但它抛出了我一个KeyError话说,“sbm_agents”表中没有找到它应该有,因为我指定绑定密钥指向SQL_SERVER绑定。

__init__.py __init__.py

from os.path import join,dirname,abspath

from flask_admin import Admin
from project.apps.flask_apps.user_app.forms import UserAdminForm
from flask_admin.contrib.sqla import ModelView

from project import app_factory, db
from project.apps.flask_apps.admin_own.views import AdminHome, Utilities
from project.apps.flask_apps.admin_own.models import  CredentCheckMappings, AllAgents, LicenseTypes
from project.apps.flask_apps.admin_own.forms import MasterAgentForm, AgentMappingsModelView, AllLicenseForm
from project.apps.flask_apps.user_app.models import Users


def create_application():
    config_path = join(dirname(abspath(__file__)), "config", "project_config.py")
    app = app_factory(config_path=config_path)
    admin = Admin(app, template_mode="bootstrap3", base_template="base_templates/admin_base.html",
              index_view=AdminHome())
    with app.app_context():
        db.create_all()
        db.Model.metadata.reflect(db.engine)
        admin.add_view(MasterAgentForm(AllAgents, db.session))
        admin.add_view(UserAdminForm(Users, db.session))
        admin.add_view(Utilities(name="Utilities", endpoint="utilities"))
        admin.add_view(AgentMappingsModelView(CredentCheckMappings, db.session))
        admin.add_view(AllLicenseForm(LicenseTypes, db.session))
    return app

application = create_application()


if __name__ == "__main__":
    application.run(host="0.0.0.0", port=5000, debug=True)

what am I missing here? 我在这里想念什么? I've tried this: flask sqlalchemy example around existing database 我试过了: 现有数据库周围的flask sqlalchemy示例

but im getting the KeyError did something change or im missing a step? 但是我收到KeyError时发生了变化还是错过了一步?

Edit: 编辑:

For people wanting to know how I got around this. 对于想知道我如何解决这个问题的人。 I went straight to SqlAlchemy and did this 我直接去了SqlAlchemy并做到了

from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.automap import automap_base
from urllib.parse import quote_plus

engine = create_engine("mssql+pyodbc:///?odbc_connect="+ quote_plus("DRIVER={FreeTDS};SERVER=172.1.1.1;PORT=1433;DATABASE=YourDB;UID=user;PWD=Password"))
metadata = MetaData(engine)
Session = scoped_session(sessionmaker(bind=engine))

LicenseType= Table("sbm_agents", metadata, autoload=True)

that way im not reflecting the entire database and only choose to reflect certain tables. 这样,我不会反映整个数据库,而只选择反映某些表。 because it turns out that reflecting the entire database is slow. 因为事实证明,反映整个数据库很慢。

although this approach is a little tougher because you have to configure FREETDS but its doable just a little tedious and confusing at first 尽管这种方法要困难一些,因为您必须配置FREETDS但是起初它有点单调乏味且令人困惑

使用db.reflect()代替db.Model.metadata.reflect(db.engine)

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

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