Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:05:25+00:00 2026-05-25T17:05:25+00:00

I need to relate two tables using SQLAlchemy 0.7; one is in a MySQL

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:05:25+00:00Added an answer on May 25, 2026 at 5:05 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create two related tables in MySQL and return a result set
I have a SQL Server 2008 database. This database has two related tables: Customer
I need to create a table (possibly a view) that relates two tables, one
I need to select data from two table using a join. This is fairly
I have two tables in an existing (MSSQL 2008 R2) database that are related
I have two tables that need to be related--a users table and an address_book
I have two tables (A and B) that need to be related. The A
I have two tables that are related, which, for the purpose of this question,
I have two very similar tables in our database, and I need to write
I have two basic interface-related concepts that I need to have a better understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.