2
votes

I used the answer to this question to fix a circular import when setting up Flask-SQLAlchemy. The solution works because you don't have to import the app from the application.py file to get the database URI - it initalizes the connection just before running the app. The issue now is that I typically like import my models file into a python interpreter just so I can test the models (and the connection to the database) without actually running the app. But with this setup, just importing models won't establish a connection to PostGres.

Is there a way to establish a connection to PostGres through Flask-SQLAlchemy without actually initializing and running an app? Maybe I can provide db with the URI to my database directly?

2

2 Answers

0
votes

Study this code https://github.com/mattupstate/overholt there is a link on github explaining the design. What should be your main focus is the usage of flask-script. I think you have to use the factory pattern, but you want to do that anyway if you are doing anything remotly serious.

Best of luck

0
votes

It looks like you are searching for this one github.com/mardix/active-alchemy

Active-Alchemy is a framework agnostic wrapper for SQLAlchemy that makes it really easy to use by implementing a simple active record like api, while it still uses the db.session underneath. Inspired by Flask-SQLAlchemy