I am working with two MySQL Databases. I want to join a table from DB 1 with a table from DB2 in SQLAlchemy.
I am using automap_base while creating data access layer in sqlalchemy as follows...
class DBHandleBase(object):
def __init__(self, connection_string='mysql+pymysql://root:xxxxxxx@localhost/services', pool_recycle=3600):
self.Base_ = automap_base()
self.engine_ = create_engine(connection_string,
pool_recycle = pool_recycle)
self.Base_.prepare(self.engine_, reflect=True)
self.session_ = Session(self.engine_)
And my tables Class is like
class T1D1_Repo():
def __init__(self, dbHandle):
# create a cursor
self.Table_ = dbHandle.Base_.classes.t1
self.session_ = dbHandle.session_
I am making the join like this,
db1_handle = DB1_Handle()
db2_handle = DB2_Handle()
t1d1_repo = T1D1_Repo(handle)
t1d2_repo = T1D2_Repo(person_handle)
result = t1d1_repo.session_.query(
t1d1_repo.Table_,
t1d2_repo.Table_).join(t1d2_repo.Table_, (
t1d1_repo.Table_.person_id
== t1d2_repo.Table_.uuid))
I am getting the error like this:
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'db1.t1d2' doesn't exist") [SQL: 'SELECT
we have created table t1 in database db1 and table t2 in database db2.
Do join is possible across two databases table in sqlalchemy ORM? How to achieve that?
In MySQL databases are synonymous with schemas. Where for example in Postgresql you can query between multiple schemas in a database, but not between databases (directly), you can query between multiple databases in MySQL as there's no distinction between the two.
In this light a possible solution to your multi-database query in MySQL could be to use a single engine, session, and Base handling both your schemas and passing the
schema
keyword argument to your tables, or reflecting both schemas so that they're fully qualified.Since I don't have your data, I made 2 schemas (MySQL databases) on a test server called sopython and sopython2:
and added a table in each:
In Python:
Create the engine without specifying which schema (database) you use by default:
The warning is something I don't fully understand, and is probably a result of the foreign key reference between the 2 tables causing re-reflecting of foo, but it does not seem to cause trouble.
The warning is the result of the second call to
prepare()
recreating and replacing the classes for the tables reflected in the first call. The way to avoid all that is to first reflect the tables from both schemas using the metadata, and then prepare:After all this you can query joining foo and bar: