最近作的工程要修改爲兩個數據庫的,一個測試數據庫, 一個線上數據庫。mysql
因此就要把原來的只有一個數據庫的改爲兩個數據庫。sql
# SQLAlchemy database URL
sqlalchemy.test.url = mysql://username:password@host:port/database
sqlalchemy.test.pool_recycle = 3600
sqlalchemy.online.url = mysql://username:password@host:port/database
sqlalchemy.online.pool_recycle = 3600
數據庫
一個測試數據庫,一個線上數據庫session
# Setup the SQLAlchemy database engine
test_engine = engine_from_config(config, 'sqlalchemy.test.')
online_engine = engine_from_config(config, 'sqlalchemy.online.')
init_model(test_engine, online_engine)app
"""SQLAlchemy Metadata and Session object"""
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
__all__ = ['BaseTest', 'BaseOnline', 'Session']
# SQLAlchemy session manager. Updated by model.init_model()
Session = scoped_session(sessionmaker())
# The declarative Base
BaseTest = declarative_base()
BaseOnline = declarative_base()測試
"""The application's model objects"""
from hello.model.meta import Session, BaseTest, BaseOnline
from hello.model.template_test import Template_test
from hello.model.template_online import Template_online
def init_model(test_engine, online_engine):
"""Call me before using any of the tables or classes in the model"""
meta.BaseTest.metadata.bind = test_engine
meta.BaseOnline.metadata.bind = online_engine
url
from sqlalchemy import Column
from sqlalchemy import types
from hello.model.meta import BaseTest
class Template_test(BaseTest):
__tablename__ = "template"spa
根據model中表的狀況修改controller的文件頭orm
from hello.model.template_test import Template_test
sqlalchemy