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

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

Open SwayamBadhe opened this issue 2 years ago • 2 comments

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

SwayamBadhe avatar Mar 06 '23 09:03 SwayamBadhe

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.

AusafMo avatar Mar 07 '23 20:03 AusafMo

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

antaeusw avatar Oct 24 '23 12:10 antaeusw