Flask-Web-App-Tutorial icon indicating copy to clipboard operation
Flask-Web-App-Tutorial copied to clipboard

TypeError: create_all() got an unexpected keyword argument 'app'

Open notaryanramani opened this issue 2 years ago • 6 comments

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path

db = SQLAlchemy()
DB_NAME = "database.db"


def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'
    app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
    db.init_app(app)

    from .views import views
    from .auth import auth

    app.register_blueprint(views, url_prefix='/')
    app.register_blueprint(auth, url_prefix='/')

    from .models import User, Note

    create_database(app)


    return app


def create_database(app):
    if not path.exists('website/' + DB_NAME):
        db.create_all(app=app)
        print('Created Database!')

notaryanramani avatar Oct 05 '22 10:10 notaryanramani

I am getting the same issue and I am using the original source code with no modifications...

ConnorDiddy avatar Oct 05 '22 14:10 ConnorDiddy

This is because flask-sqlalchemy update in 3.0 look up here https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/quickstart/#create-the-tables

update your code to this

def create_database(app):
    if not path.exists('website/' + DB_NAME):
        with app.app_context():
            db.create_all()
            print('Created Database!')
            

your database will created in folder \instance\database.db

ammar0466 avatar Oct 06 '22 02:10 ammar0466

This is because flask-sqlalchemy update in 3.0 look up here https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/quickstart/#create-the-tables

update your code to this

def create_database(app):
    if not path.exists('website/' + DB_NAME):
        with app.app_context():
            db.create_all()
            print('Created Database!')
            

your database will created in folder \instance\database.db

Thank you so much, was getting very confused myself.

BrokkrDwarf avatar Oct 09 '22 18:10 BrokkrDwarf

thanks dude, u saved my life... XD

berk-efe avatar Oct 21 '22 19:10 berk-efe

thanks for sharing. My db is created in a folder called: instance folder anyone kow why and how to revert it to the website folder?

Luluam avatar Dec 06 '22 00:12 Luluam

Thank you for the help.. Solved my problem

Syntx-09 avatar Jan 25 '23 20:01 Syntx-09