Flask-SQLAlchemy: KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

Python logó
Probléma: Flask-SQLAlchemy konfigurálásakor a app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False (vagy True, mindegy) sor hatására a következő hibaüzenetet kapod az első lekérdezés futtatásakor:

  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 137, in __init__
    track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS

Bővebben:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/_collections.py", line 1030, in __call__
    return self.registry[key]
KeyError: <greenlet.greenlet object at 0x7f062c1d1cc0>
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "testpy.py", line 91, in index
    db.session.add(admin)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/_collections.py", line 1032, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/session.py", line 3254, in __call__
    return self.class_(**local_kw)
  File "/home/lacyc3/.local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 137, in __init__
    track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

Megoldás: Minden bizonnyal két app = Flask("alkalmazás_neve") sor van a kódodban. A második pedig minden bizonnyal a db = SQLAlchemy(app) sor után.
Csak 1db app = Flask("alkalmazás_neve") sor maradjon, praktikusan az SQLAlchemy előtti.