2

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?

Community
  • 1
  • 1
J-bob
  • 8,380
  • 11
  • 52
  • 85

2 Answers2

0

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

brunsgaard
  • 5,066
  • 2
  • 16
  • 15
0

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

Levon
  • 10,408
  • 4
  • 47
  • 42
  • 1
    You can't really mix it with Flask-SQLAlchemy. It adds these records 'created_at', 'deleted_at' which will clash. I'm realizing you have to replace Flask-SQLAlchemy with Active-Alchemy entirely which I wasn't really expecting. – mtourne Oct 10 '16 at 19:25