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 6579271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:38+00:00 2026-05-25T15:52:38+00:00

It took me a while, but I figured out how to use SQLAlchemy to

  • 0

It took me a while, but I figured out how to use SQLAlchemy to model a relationship across two different kinds of databases:

    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

I have to do this because the TERM table is in an Oracle database on which I only have read access, and I’m writing an app that records surveys, taken by students, about that term.

The above works, but it’s very fragile when the number of tables climbs up, as the Session needs to specify exactly which mapped classes correspond to which engine. I would really like to be able to use a different Base to define which tables belong to which engine, instead of binding each table individually. Like this:

    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

Unfortunately, this results in the following error when the query runs:

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.

even though I did add the relationship() to Survey after Term was defined.

Does anyone have any suggestions?

  • 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-25T15:52:39+00:00Added an answer on May 25, 2026 at 3:52 pm

    You can’t. AFAIK there’s no single query against two different databases. Also, your Models have to share the same Metadata instance to be used in the same query.

    Perhaps you can link the Oracle db to the MySQL db on the DB layer via ODBC, then you’d only talk to MySQL. I have never done this, and I don’t know how it works.

    You can also query both databases independently and filter and select data on the application layer, whichever is less work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, it took me a little while to narrow down this problem, but it
This took me a while to figure out and I found the answer on
I have two xml files which have the same data but different names for
Took me a while to figure this out. So, I am answering my own
I have the following text from an academic course I took a while ago
It took me a while to realize what was going on with mouse events
I've put off WPF development for a quite a while but, I'm finally thinking
Took me a while to find this bug in my code. Trying to create
This bug took me a while to find... Consider this method: public void foo(Set<Object>
I took a data structures class in C++ last year, and consequently implemented all

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.