Halcyon Abraham Ramirez
Halcyon Abraham Ramirez

Reputation: 1560

Flask-SQLAlchemy multiple databases and binds

URI:

SQLALCHEMY_DATABASE_URI = "mssql+pymssql://user:[email protected]/DbOne"


SQLALCHEMY_BINDS = {
    "sql_server": "mysql+pymysql://user:[email protected]/DbTwo"
}

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.

__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

but im getting the KeyError did something change or im missing a step?

Edit:

For people wanting to know how I got around this. I went straight to SqlAlchemy and did this

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

Upvotes: 4

Views: 6563

Answers (1)

ichbinblau
ichbinblau

Reputation: 4819

Use db.reflect() instead of db.Model.metadata.reflect(db.engine).

Upvotes: 1

Related Questions