python - Sqlalchemy db.create_all() not creating tables -


i following this pattern avoid circular imports. pattern seems pretty niche it's been difficult trying find solution googling.

models.py

from sqlalchemy.orm import relationship  db = sqlalchemy()  class choice(db.model):   id = db.column(db.integer, primary_key=true)   text = db.column(db.string(32))   votes = relationship("vote")    def __init__(self, text):     self.text = text 

application.py

app = flask(__name__) app.debug = true  basedir = os.path.abspath(os.path.dirname(__file__)) app.config['sqlalchemy_database_uri'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3') app.config['sqlalchemy_track_modifications'] = false db.init_app(app)      # reinitialiez db.model avoid cyclical imports in models.py 

db_create.py

#!flask/bin/python application import app, db models import choice  db.init_app(app) db.create_all()  db.session.add(choice("red")) db.session.add(choice("blue")) db.session.add(choice("green"))  db.session.commit() 

when run db_create.py separately, get:

$ python db_create.py traceback (most recent call last):   file "db_create.py", line 6, in <module>     db.create_all()   file "/home/ubuntu/.virtualenvs/paw-py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_all     self._execute_for_all_tables(app, bind, 'create_all')   file "/home/ubuntu/.virtualenvs/paw-py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 949, in _execute_for_all_tables     app = self.get_app(app)   file "/home/ubuntu/.virtualenvs/paw-py/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 922, in get_app     raise runtimeerror('application not registered on db ' runtimeerror: application not registered on db instance , no application bound current context 

what should do? best pattern deal this? i've tried adding db.create_all() in application.py after if __name__ == '__main__' still getting same error

tell flask-sqlalchemy "current" app app_context

with app.app_context():     # code here     db.create_all() 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -