SQLAlchemy:使用不同类型键的表之间的关系?

发布于 2024-12-05 09:07:37 字数 2135 浏览 1 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-12-12 09:07:37

我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文