SQLAlchemy:使用不同类型键的表之间的关系?
我需要使用 SQLAlchemy 0.7 关联两个表;一个是MySQL数据库,另一个是Oracle数据库。
我已经成功关联了这样的表,其中键的类型相同:
Base = declarative_base()
class Survey(Base):
__tablename__ = 'SURVEY'
survey_id = Column(Integer, primary_key=True)
term_id = Column(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
}
))
但是,我遇到了一个障碍,其中 Oracle 表的主键之一 (PERSON.person_id
) 是 < code>VARCHAR2(30),MySQL 表中的相关键 (ANSWER.person_id
) 为 INT 类型。我无法更改 Oracle 表,并且我宁愿避免更改 MySQL 表。当我尝试通过 ANSWER
上的关系检索 PERSON
对象时,Oracle 会抛出:
ORA-01722: invalid number
这似乎是因为它正在尝试类似于以下内容的查询:
SELECT * from PERSON where person_id = 12345;
而不是
SELECT * from PERSON where person_id = '12345';
So, What I'我正在寻找一种方法来告诉 SQLAlchemy 在针对 Oracle 表执行的查询中使用ANSWER.person_id
之前应将其转换为字符串。我尝试使用 SQLAlchemy 的 func 构造,但是:
Answer.person = relationship(Person,
primaryjoin=Person.person_id == func.TO_CHAR(Answer.person_id),
foreign_keys=[Answer.person_id]
)
导致 SQLAlchemy 引发此错误:
sqlalchemy.exc.ArgumentError:无法确定 Primaryjoin 条件 'PERSON.person_id = TO_CHAR( ANSWER.person_id)',关于关系 Answer.person,使用手动“foreign_keys”设置。 “foreign_keys”中的列是否代表此连接条件中的所有且仅“外部”列?映射表是否已经建立了足够的ForeignKey和/或ForeignKeyConstraint对象(在这种情况下,“foreign_keys”通常是不必要的)?
任何想法将不胜感激!
I need to relate two tables using SQLAlchemy 0.7; one is in a MySQL database, and the other is in an Oracle database.
I've already related tables like this successfully where the keys are the same type:
Base = declarative_base()
class Survey(Base):
__tablename__ = 'SURVEY'
survey_id = Column(Integer, primary_key=True)
term_id = Column(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
}
))
However, I've run into a snag where one of the Oracle tables' primary keys (PERSON.person_id
) is a VARCHAR2(30)
, and the related key on the MySQL table (ANSWER.person_id
) is type INT. I can't alter the Oracle table, and I'd prefer to avoid altering the MySQL table. When I try to retrieve the PERSON
object through the relationship on ANSWER
, Oracle throws:
ORA-01722: invalid number
which appears to be because it's trying a query similar to:
SELECT * from PERSON where person_id = 12345;
instead of
SELECT * from PERSON where person_id = '12345';
So, what I'm looking for is a way to tell SQLAlchemy that ANSWER.person_id
should be converted to a string before using it in the queries it does against the Oracle table. I've tried to use SQLAlchemy's func
construct, but:
Answer.person = relationship(Person,
primaryjoin=Person.person_id == func.TO_CHAR(Answer.person_id),
foreign_keys=[Answer.person_id]
)
causes SQLAlchemy to raise this error:
sqlalchemy.exc.ArgumentError: Could not determine relationship direction for primaryjoin condition 'PERSON.person_id = TO_CHAR(ANSWER.person_id)', on relationship Answer.person, using manual 'foreign_keys' setting. Do the columns in 'foreign_keys' represent all, and only, the 'foreign' columns in this join condition? Does the mapped Table already have adequate ForeignKey and/or ForeignKeyConstraint objects established (in which case 'foreign_keys' is usually unnecessary)?
Any ideas would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 sqlalchemy Google Group 上问了同样的问题,并得到了一位作者的回复,并提供了一个可行的解决方案。如果您有兴趣建立这种关系,请查看他添加到 sqlachemy wiki 的新页面:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/RelationshipOnCast
I asked this same question over on the sqlalchemy Google Group, and got a response from one of the authors with a working solution. If you're interested in doing this kind of relationship, check out the new page he added to the sqlachemy wiki:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/RelationshipOnCast