1

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

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • It is a strange behavior of Sqlalchemy. We faced it while dropping the models/tables and we also had to import the model. Commenting to follow the post. – mad_ Jul 03 '18 at 13:54
  • The imports are not superfluous and when you think about it there's nothing strange. You have to import all model classes before using your models, because without importing, the class definitions are never evaluated, or in other words the code is not executed, and so the models and their tables are not created and stored in the registries. – Ilja Everilä Jul 04 '18 at 06:46
  • @IljaEverilä thanks for your comment - I suspected it would be something like that. If you'd like to make this an answer I'll accept it. If you could provide a link to any docs / guidelines for structuring models spread across multiple packages that would be particularly helpful. – snakecharmerb Jul 04 '18 at 07:57
  • Was trying to find if this has been addressed before here in SO. [Here's](https://stackoverflow.com/questions/42914302/flask-sqlalchemy-create-all-without-having-to-import-models) a somewhat related Q/A. The case is a bit different, but the underlying issue the same. – Ilja Everilä Jul 04 '18 at 18:22
  • Here's some talk about structuring modules: https://stackoverflow.com/questions/29217076/flask-sqlalchemy-query-have-to-import-all-models – Ilja Everilä Jul 04 '18 at 19:12
  • https://stackoverflow.com/questions/10345698/how-do-i-split-models-py-into-different-files-for-different-models-in-pyramid – Ilja Everilä Jul 04 '18 at 19:44
  • @IljaEverilä thanks for your comments so far; I have to go offline for a few days but will pick up again after that. – snakecharmerb Jul 04 '18 at 20:53

0 Answers0