我花了一段时间,但我想出了如何使用SQLAlchemy来模拟两种不同类型数据库之间的关系:
Base = declarative_base()
class Survey(Base):
__tablename__ = 'SURVEY'
survey_id = Column("SURVEY_ID", Integer, primary_key=True)
term_id = Column("TERM_ID", Integer, nullable=False)
# Because the TERM table is in Oracle, but the SURVEY table is in
# MySQL, I can't rely on SQLAlchemy's ForeignKey. Thus,
# I need to specify the relationship entirely by hand, like so:
term = relationship("Term",
primaryjoin="Term.term_id==Survey.term_id",
foreign_keys=[term_id],
backref="surveys"
)
class Term(Base):
__tablename__ = 'TERM'
term_id = Column(Integer, primary_key=True)
term_name = Column(String(30))
start_date = Column(Date)
end_date = Column(Date)
mysql_engine = create_engine(MYSQL)
oracle_engine = create_engine(ORACLE)
Session = scoped_session(sessionmaker(
binds={
Term: oracle_engine,
Survey: mysql_engine
}
))
if __name__ == "__main__":
survey = Session.query(Survey).filter_by(survey_id=8).one()
print survey.term
print survey.term.surveys
我必须这样做,因为TERM表位于我只有读取权限的Oracle数据库中,我正在编写一个应用程序来记录学生对该术语的调查。
上述工作,但是当表的数量增加时它非常脆弱,因为Session需要准确指定哪个映射类对应于哪个引擎。我真的希望能够使用不同的Base
来定义哪些表属于哪个引擎,而不是单独绑定每个表。像这样:
mysql_engine = create_engine(MYSQL)
oracle_engine = create_engine(ORACLE)
MySQLBase = declarative_base(bind=mysql_engine)
OracleBase = declarative_base(bind=oracle_engine)
class Survey(MySQLBase):
__tablename__ = 'SURVEY'
survey_id = Column("SURVEY_ID", Integer, primary_key=True)
term_id = Column("TERM_ID", Integer, nullable=False)
class Term(OracleBase):
__tablename__ = 'ads_term_v'
term_id = Column(Integer, primary_key=True)
term_name = Column(String(30))
start_date = Column(Date)
end_date = Column(Date)
Survey.term = relationship("Term",
primaryjoin="Term.term_id==Survey.term_id",
foreign_keys=[Survey.term_id],
backref="surveys"
)
Session = scoped_session(sessionmaker())
if __name__ == "__main__":
survey = Session.query(Survey).filter_by(survey_id=8).one()
print survey.term
print survey.term.surveys
不幸的是,这会在查询运行时导致以下错误:
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|Survey|SURVEY, expression 'Term.term_id==Survey.term_id' failed to locate a name ("name 'Term' is not defined"). If this is a class name, consider adding this relationship() to the <class '__main__.Survey'> class after both dependent classes have been defined.
即使我确实在定义术语后将关系()添加到Survey。
有人有任何建议吗?
答案 0 :(得分:5)
这个回复可能很晚,但您可以将声明基础中的元数据分开定义,然后将其传递给两者。即:
meta = MetaData()
mysql_engine = create_engine(MYSQL)
oracle_engine = create_engine(ORACLE)
MySQLBase = declarative_base(bind=mysql_engine, metadata=meta)
OracleBase = declarative_base(bind=oracle_engine, metadata=meta)
答案 1 :(得分:3)
你做不到。 AFAIK对两个不同的数据库没有单一查询。此外,您的模型必须共享要在同一查询中使用的相同元数据实例。
也许您可以通过ODBC将Oracle数据库链接到数据库层上的MySQL数据库,然后您只能与MySQL通信。我从来没有这样做过,我不知道它是如何运作的。
您还可以单独查询这两个数据库,并在应用程序层上过滤和选择数据,以较少的工作量为准。