before I start contriving a minimal example with a lot of sqlalchemy boilerplate stuff, maybe I can explain the concept theoretically.
I have a package "foo" that defines some tables in a database schema "foo_db" in the standard ORM manner:
class FooTable(Base):
__tablename__ = 'foo_data'
id = Column(Integer, primary_key=True)
The package is typically used stand-alone with its own database schema, "foo_db."
A separate package "bar" has its own schema "bar_db" with its own tables, but it also needs to use the tables of "foo" on "foo_db," and it has foreign keys into foo_db's tables (both schemas obviously are on the same server):
from foo.models import Base, FooTable
class BarTable(Base):
__tablename__ = 'bar_data'
id = Column(Integer, primary_key=True)
foo_id = Column(ForeignKey('foo_db.foo_data.id'))
foo = relationship(FooTable)
When I try to use this code I get errors like these:
(MySQLdb._exceptions.ProgrammingError) (1146, "Table 'bar_db.foo_data' doesn't exist")
The only way I found to get around this is to literally re-define FooTable in package bar:
class FooTable(Base):
__tablename__ = 'foo_data'
__table_args__ = {'schema': 'foo_db'}
id = Column(Integer, primary_key=True)
This is very obviously not how it should be done. Any suggestions?