librelist archives

« back to archive

more app factory help

more app factory help

From:
Matthew Hoopes
Date:
2013-08-26 @ 14:27
Hi,

I was recently helped on this list (

http://librelist.com/browser//flask/2013/8/21/switching-sqlalchemy-binds-on-the-fly/),
and I was hoping for a bit more advice.

I've currently got a create_app function for my application factory, and
have been trying to untangle imports so nothing imported needs the config
before I have a proper context.

My current problem is that I have some models that live in databases that
are doing cross-database joins. Similar to
http://stackoverflow.com/questions/6433592/cross-database-join-in-sqlalchemyand

http://stackoverflow.com/questions/16567448/cross-database-join-in-flask-sqlalchemy.
The problem is that my schema name may change based on the config, so
I
don't have it when attempting to define the models, which are imported and
used in a view. This view is imported before the application context is
created - so I have a chicken-and-egg problem.

Re-stated: The view is imported in create_app, which imports the model, but
the model needs the config from the app context, which isn't created until
after create_app...

(Note - I could be missing something obvious here)

I've also tried the appcontext_pushed signal, but it looks like routing
might have to be set up before that?

Another solution to the problem might also be forcing sqlalchemy to always
emit the schema name when building the query, and not set the schema name
in the __table_args__ manually. If anyone has any advice on that, I'd be
much obliged.

Anyway - any help is greatly appreciated, as always!