techwithtim / Flask-Web-App-Tutorial

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

SQLAlchemy.create_all() got an unexpected keyword argument 'app' #111

Open SwayamBadhe opened 1 year ago

SwayamBadhe commented 1 year ago

image

SQLAlchemy.create_all() got an unexpected keyword argument 'app'

`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'] = 'kjsdhfkjashfkh' 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!')`

AusafMo commented 1 year ago

You no longer have to manually check if path.exists() in flask, instead do this

with app.app_context(): db.create_all()

this checks it for you, I believe it's already updated in the git, auth.py file though.

antaeusw commented 10 months ago

This is explained well in this StackOverflow discussion: https://stackoverflow.com/questions/73968584/flask-sqlalchemy-db-create-all-got-an-unexpected-keyword-argument-app