2

I am currently working on the user management part of an application through Python while using Flask web framework, and I need a bit of help creating a SQLite database through SQLAlchemy.

Here's a snippet from the __init__.py file from my backend package.

from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from backend.config import Config

db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message_category = 'info'
mail = Mail()

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)

    from backend.users.routes import users
    from backend.stats.routes import stats
    from backend.queues.routes import queues
    app.register_blueprint(users)
    app.register_blueprint(stats)
    app.register_blueprint(queues)

    return app

Here's a snippet from my main run.py file.

from backend import create_app

app = create_app()

if __name__ == '__main__':
    app.run(host = '127.0.0.1', port = 80, debug = True)

The config of the app refers the SQLALCHEMY_DATABASE_URI to be 'sqlite:///site.db'.

I have tried pushing the app through app_context. I have tried creating the database tables within it (to be frank, I have tried literally all of the workarounds I could find over the internet). Does anyone have any suggestions as to how I can tackle this problem (apart from creating the tables manually -- doing that does work, but that does not seem to be a long-term solution).

As a quick note, here's the error I am receiving.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 963, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 940, in _execute_for_all_tables
    app = self.get_app(app)
  File "C:\Users\Rachit Bhargava\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 912, in get_app
    'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
Viktor Ilienko
  • 817
  • 8
  • 15
Rachit Bhargava
  • 170
  • 1
  • 12
  • Try commenting out parts of that until you can get to the smallest app that demonstrates the problem. – Dave W. Smith Nov 06 '18 at 22:27
  • Do you mean commenting out all the other parts that reference the app to objects like bcrypt and login_manager? – Rachit Bhargava Nov 07 '18 at 04:53
  • From the looks of it you've imported `db` in a Python shell and tried to run `db.create_all()`, but without having an application context. In other words the code you've presented is not what is actually causing the error. See if https://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w provides and help. – Ilja Everilä Nov 07 '18 at 11:33
  • Well, I have tried that before, but it does not seem to work. The error simply remains the same. – Rachit Bhargava Nov 07 '18 at 18:21
  • It looks like there's a lot going on there that may have nothing to do with the problem. Commenting out bits of code until the problem goes away can be very informative. And it leaves you with a simpler example to post, which makes it easier for people to help. – Dave W. Smith Nov 07 '18 at 23:02
  • Hi @DaveW.Smith, thanks for the awesome suggestion! I just tried doing it, but it does not seem to bring in any changes. – Rachit Bhargava Nov 08 '18 at 07:03

1 Answers1

0

You need to provide application context to SqlAlchemy constructor. from run import app db = SQLAlchemy(app)

Haseeb Mazhar Ranga
  • 555
  • 1
  • 5
  • 16