2016-12-16 76 views
1

URI:燒瓶SQLAlchemy的多個數據庫和結合

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"] 

但它拋出我KeyError稱 'sbm_agents' 表中沒有找到它應該在那裏,因爲我已經指定綁定鍵指向sql_server綁定。

__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) 

我缺少什麼嗎? 我已經試過這樣: flask sqlalchemy example around existing database

但即時得到KeyError 做了一些改變或IM缺少一個步驟?

編輯:

對於人們想知道我解決這個怎麼得到。 我直奔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) 

方式IM,不是反映了整個數據庫,只能選擇以反映特定表。因爲反映整個數據庫的速度很慢。

雖然這種做法是有點艱難,因爲你必須配置FREETDS,但其可行只是有點乏味和混亂在第一

回答

0

使用db.reflect(),而不是db.Model.metadata.reflect(db.engine)

+0

我仍然不會讓我......我不知道爲什麼,但每當我在'LicenseType'模型上執行'db.Model.metadata.tables.keys()'時,我仍然得到'keys'儘管已經指定了綁定密鑰,但默認的'URI' ..我通過完全繞過flask-sqlalchemy並直接進入sqlalchemy –