I have models declared in different packages, like this:
base.models.py
class BaseModel(object):
__abstract__ = True
id = sa.Column(sa.Integer, primary_key=True)
MyBase = declarative.declarative_base(cls=BaseModel, metadata=metadata)
foo.models.py
class FooModel(MyBase):
__tablename__ = 'foos'
leads = sa.orm.relationship('bar.models.Bar', backref='foo')
bar.models.py
class Bar(MyBase):
__tablename__ = 'bars'
foo_id = sa.Column(sa.Integer, sa.ForeignKey('foos.id'))
If I query Bar
,
count = session.query(Bar).filter_by(some_attr='spam').count()
Sqlalchemy raises an exception:
NoReferencedTableError: Foreign key associated with column 'bars.foo_id' could not find table 'foos' with which to generate a foreign key to target column 'id'
I can work around this by importing Foo
in the module where I'm executing the query.
Is there a way to configure the models so that this exception doesn't occur, and I don't have to add superfluous imports?
sqlalchemy==1.2.5