1

I have built a flask web application using some tutorial on youtube. Its was working fine till I tried to add new tables to the db. I created the table class in models.py. But when I try to run db.create_all() from the python console after running from myapp import db, I get this error:

RuntimeError: No application found. Either work inside a view function or push an application context.

I have checked the similar questions but I couldn't find a clear answer. Everyone expects some level of proficiency with flask which apparently I don't have yet. That's the reason I am seeking help here. I think the problem is that imported db has no idea about models. I don't know how to let it know about classes in models. Please help!

This is how myapp/init.py file looks:

    #IMPORTING NECESSARY LIBRARIES TO CREATE USEFUL OBJECTS

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

# CREATING USEFUL OBJECTS FOR USAGE IN OUR APP
mail = Mail()
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'


def create_app(config_class=Config):
    # creating app variable out of Flask class
    app = Flask(__name__) 
    app.config.from_object(Config)

    # CONNECTING USEFUL OBJECTS WITH FLASK OBJECT APP
    mail.init_app(app)
    db.init_app(app)
    bcrypt.init_app(app)
    login_manager.init_app(app)

    # ADDING ROUTES TO THE FLASK OBJECT APP
    from myapp.users.routes import users
    from myapp.posts.routes import posts
    from myapp.main.routes import main
    from myapp.errors.handlers import errors
    from myapp.finance.routes import finance_data

    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)
    app.register_blueprint(errors)
    app.register_blueprint(finance_data)

    return app

This is how myapp/models.py file looks:

from myapp import db, login_manager
from datetime import datetime
from flask_login import UserMixin
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
import time

today = datetime.today()
datem = datetime(today.year, today.month, 1)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post',backref='author', lazy=True)
    fin = db.relationship('Finance', backref='author', lazy=True)

    def __repr__(self):
        return f"User('{self.username}','{self.email}','{self.image_file}')"

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id':self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])    
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    def __repr__(self):
        return f"Post('{self.title}','{self.date_posted}')"


class Finance(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    month = db.Column(db.DateTime, nullable=False, default=datem)
    revenue = db.Column(db.Float, nullable=False,default=0)
    Other_income = db.Column(db.Float,nullable=False,default=0)
    total_income = db.Column(db.Float, nullable=False,default=0)
    cogs = db.Column(db.Float,nullable=False,default=0)
    gross_profit = db.Column(db.Float, nullable=False,default=0)
    sga_expenses = db.Column(db.Float,nullable=False,default=0)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Finance('{self.month}','{self.user_id}')"

This is how the run.py file looks:

from myapp import create_app

app = create_app()


if __name__ == '__main__':  
    app.run(debug=True)
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • Does this answer your question? ['No application found. Either work inside a view function or push an application context.'](https://stackoverflow.com/questions/46540664/no-application-found-either-work-inside-a-view-function-or-push-an-application) – rfkortekaas Jun 22 '21 at 07:44
  • What you can see in the tutorial https://pythonbasics.org/flask-sqlalchemy/ is that they provide the app to sqlAlchemy, like this: 'db = SQLAlchemy(app)', that might be it to be honest – T. Kelher Jun 22 '21 at 08:00

1 Answers1

3

you need to use app_context for it, try this in terminal, open terminal in the folder where run.py is:

from myapp import create_app, db
app = create_app()
with app.app_context():
    db.create_all()
     
charchit
  • 1,492
  • 2
  • 6
  • 17
  • It has worked but I am still clueless how it worked. Your solution never used myapp.models package where I defined the table classes. Then how db.create_all() created the tables? Could you please explain a bit? – KawaiKx Jun 22 '21 at 10:50
  • 1
    you used `db.init_app(app)` did the task, and when you run create_app() function, the database in initialized. and of app.app_context() you can look [here](https://flask.palletsprojects.com/en/2.0.x/appcontext/) – charchit Jun 22 '21 at 11:14
  • 1
    slight correction to your explanation @charchit I found that app object learns about the models.py file while importing a blueprint which relies on models.py. db.init_app(app) had nothing to do with models.py. I went to github flask-sqlalchemy source code. def init_app() function doesn't deal with models.py https://github.com/pallets/flask-sqlalchemy/blob/99e1937acf0f7f14964495e774b84034057c83bb/src/flask_sqlalchemy/__init__.py#L844 – KawaiKx Jun 25 '21 at 12:40