How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

Each time I run my application using Flask-SQLAlchemy, I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS parameter will be disabled.

 /home/david/.virtualenvs/flask-sqlalchemy/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') 

I tried to figure out what this option does, but the Flask-SQLAlchemy documentation is not clear about what this tracking uses.

SQLALCHEMY_TRACK_MODIFICATIONS

If set to True (the default), the SQLAlchemy checkbox will track object changes and emit signals. This requires additional memory and can be disabled if not required.

How do I know if my project SQLALCHEMY_TRACK_MODIFICATIONS = True , or if I can safely disable this function and save memory on my server?

+67
python flask flask-sqlalchemy sqlalchemy
Nov 16 '15 at 15:05
source share
3 answers

Most likely, your application does not use the Flask-SQLAlchemy event system, so you can probably disable it. You will need to check the code to check - you are looking for anything that connects to models_committed or before_models_committed . If you find yourself using the Flask-SQLAlchemy event system, you should probably update your code to use the SQLAlchemy built-in event system.

To disable the Flask-SQLAlchemy event system (and disable the warning), simply add SQLALCHEMY_TRACK_MODIFICATIONS = False to the application configuration until the default value is changed (most likely in Flask-SQLAlchemy v3).

Background is what warns you:

Flask-SQLAlchemy has its own event notification system, which is superimposed on top of SQLAlchemy. To do this, it tracks changes to the SQLAlchemy session. This requires additional resources, so the SQLALCHEMY_TRACK_MODIFICATIONS option allows SQLALCHEMY_TRACK_MODIFICATIONS to disable the change tracking system. Currently, the default setting is True , but in the future, this default value will change to False , thereby disabling the event system.

As I understand it, the rationale for the change is three times:

  • Not many people use the Flask-SQLAlchemy event system, but most people do not realize that they can save system resources by disabling it. Thus, by default it is to disable it, and those who want it can enable it.

  • The event system in Flask-SQLAlchemy was rather erroneous (see problems associated with the pull request below), requiring additional maintenance for a function that few people use.

  • In v0.7, SQLAlchemy itself added a powerful event system, including the ability to create custom events. Ideally, the Flask-SQLAlchemy event system should do nothing more than create multiple custom event hooks and SQLAlchemy listeners, and then allow SQLAlchemy to independently control the event trigger.

You can see more in the discussion of the pull request that started to raise this warning .

+92
Nov 18 '15 at 20:56
source share

Jeff Widman's detailed explanation is just perfect.

Since I had some kind of copy of the battle, before getting this right, I would like to facilitate the next one that will be in my shoes.

In your code, after:

 app = Flask(__name__) 

If you want to enable track changes just add:

 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True 

Otherwise, if you do not use this function, you can change the value to False, so as not to waste system resources. This will disable the warning anyway, since you are explicitly setting the configuration anyway.

Here's the same snippet with a False value:

 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 

Thanks to Jeff Widman for this added suggestion and details.

+24
Sep 30 '16 at 14:59
source share

The above answers look good. However, I wanted to indicate this line in the Flask-SQLAlchemy documentation, because I still received these warnings after setting SQLALCHEMY_TRACK_MODIFICATIONS = False in my application configuration.

On this page: http://flask-sqlalchemy.pocoo.org/2.3/config/

For Flask-SQLAlchemy, the following configuration values ​​exist. Flask-SQLAlchemy loads these values ​​from your main Flask configuration, which can be populated in various ways. Please note that some of them cannot be changed after creating the engine, so be sure to configure them as early as possible and do not change them at runtime.

In other words, before creating the Flask-SQLAlchemy database, make sure you create app.config .

For example, if you are setting up an application to set SQLALCHEMY_TRACK_MODIFICATIONS = False :

 from flask import Flask app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) 
+2
Oct 05 '17 at 3:40
source share



All Articles