Jack022
Jack022

Reputation: 1297

Flask throwing exception when i run my app

I started a simple Flask project, i'm using SLQAlchemy to handle my Database. My problem is that every time i run my app, i'll get the following error:

File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 137, in __init__
    track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

Here is my code:

from flask import Flask, request, jsonify
import json
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String
from sqlalchemy.schema import FetchedValue

app = Flask(__name__)

db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'users'
    __table_args__ = {'schema': 'tvtg'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(350), nullable=False)
    email = db.Column(db.String(350), nullable=False)
    password = db.Column(db.String(350), nullable=False)
    


@app.route('/')
def hello_world():
    print('HERE')
    peter = User.query.filter_by(name='peter').first()
    print(peter)
    return 'hello_world'


if __name__ == "__main__":
    app.run()

Can anyone help me find what i'm doing wrong? The traceback of the error is not helping me much

Upvotes: 0

Views: 243

Answers (1)

Jürgen Gmach
Jürgen Gmach

Reputation: 6131

Actually, it looks like when using flask-sqlalchemy, you have to set a value for SQLALCHEMY_TRACK_MODIFICATIONS - even the documentation says otherwise (defaults to None).

Whether you want to set it to True or False is up to your use case, see documentation

https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/

Or have a look at this great tutorial

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database

The unreleased Flask-Sqlalchemy version 3 sets a new default of False.

Upvotes: 1

Related Questions