开发者

SQLAlchemy: relationship between tables using keys of different types?

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_i开发者_如何学God = 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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜