我正在使用sqlalchemy orm自动映射来映射我现有的mysql表。我正在使用三个数据库,在我的项目中数据库之间存在交叉关系。我有三个DBHandler类。处理程序之一如下所示,
class EngineDBHandle(DBHandleBase):
def __init__(self):
self.Base_ = automap_base()
self.engine_ = create_engine('mysql+pymysql://root:xxxxxxx' \
'@localhost/my_db?charset=utf8', \
pool_recycle=3600,encoding='utf-8')
self.Base_.prepare(self.engine_,
reflect=True,
name_for_scalar_relationship=self.name_for_scalar_relationship)
self.session_ = Session(self.engine_)
Run Code Online (Sandbox Code Playgroud)
我创建了表存储库类来访问单个表,例如
class CompanyRepo(BaseRepo):
def __init__(self, dbHandle):
# create a cursor
self.Table_ = dbHandle.Base_.classes.companies
self.session_ = dbHandle.session_
Run Code Online (Sandbox Code Playgroud)
当我尝试使用APScheduler访问表类时,有时我的DAL失败,并显示以下错误代码,
One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|users|users'. Original exception was: Class 'sqlalchemy.ext.automap.subscription_plan' is not mapped
Job "e4a02f60-7d95-11e7-8314-18a6f713ce75 (trigger: date[2017-08-10 12:05:25 IST], next run at: 2017-08-10 12:05:25 IST)" raised an exception …Run Code Online (Sandbox Code Playgroud) 我正在使用两个MySQL数据库.我想在DBAlchemy中加入来自DB 1的表和DB2中的表.
我在sqlalchemy中创建数据访问层时使用的是automap_base,如下所示......
class DBHandleBase(object):
def __init__(self, connection_string='mysql+pymysql://root:xxxxxxx@localhost/services', pool_recycle=3600):
self.Base_ = automap_base()
self.engine_ = create_engine(connection_string,
pool_recycle = pool_recycle)
self.Base_.prepare(self.engine_, reflect=True)
self.session_ = Session(self.engine_)
Run Code Online (Sandbox Code Playgroud)
我的桌子类就像
class T1D1_Repo():
def __init__(self, dbHandle):
# create a cursor
self.Table_ = dbHandle.Base_.classes.t1
self.session_ = dbHandle.session_
Run Code Online (Sandbox Code Playgroud)
我正在这样做,
db1_handle = DB1_Handle()
db2_handle = DB2_Handle()
t1d1_repo = T1D1_Repo(handle)
t1d2_repo = T1D2_Repo(person_handle)
result = t1d1_repo.session_.query(
t1d1_repo.Table_,
t1d2_repo.Table_).join(t1d2_repo.Table_, (
t1d1_repo.Table_.person_id
== t1d2_repo.Table_.uuid))
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'db1.t1d2' doesn't exist") [SQL: 'SELECT
Run Code Online (Sandbox Code Playgroud)
我们在数据库db1中创建了表t1,在数据库db2中创建了表t2.
是否可以在sqlalchemy ORM中的两个数据库表中进行连接?怎么实现呢?