techwithtim / Flask-Web-App-Tutorial

Code for the note storing flask web app made during a YouTube video.
932 stars 1.03k forks source link

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

Open notaryanramani opened 2 years ago

notaryanramani commented 2 years ago
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!')
ConnorDiddy commented 2 years ago

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

ammar0466 commented 2 years ago

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

BrokkrDwarf commented 2 years ago

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.

berk-efe commented 2 years ago

thanks dude, u saved my life... XD

Luluam commented 1 year ago

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?

Syntx-09 commented 1 year ago

Thank you for the help.. Solved my problem