'How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?
Every time I run my app that uses Flask-SQLAlchemy I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS option 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 find out what this option does, but the Flask-SQLAlchemy documentation isn't clear about what uses this tracking.
SQLALCHEMY_TRACK_MODIFICATIONSIf set to True (the default) Flask-SQLAlchemy will track modifications of objects and emit signals. This requires extra memory and can be disabled if not needed.
How do I find out if my project requires SQLALCHEMY_TRACK_MODIFICATIONS = True or if I can safely disable this feature and save memory on my server?
Solution 1:[1]
Jeff Widman's detailed explanation is simply perfect.
Since I had some copy'n'paste fights before getting this right I'd like to make it easier for the next one that will be in my shoes.
The needed configuration must be added in your code, between:
app = Flask(__name__)
and:
db = SQLAlchemy(app)
If you want to enable track modifications add:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
Otherwise, if you are not using this feature, you may want to change the value to False:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
This will silence the warning anyway since you're explicitly setting the parameter in your configuration.
The final result should look similar to this example:
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Note
Please keep in mind that SQLALCHEMY_TRACK_MODIFICATIONS defaults to None so there is no risk of memory loss related if you don't configure it.
Anyhow, if you want to suppress the warning, you will have to choose between True and False in your config.
Thanks to Jeff Widman for this added suggestion and details.
Solution 2:[2]
The above answers look good. However, I wanted to point out this line in the Flask-SQLAlchemy documentation because I was still getting these warnings after setting SQLALCHEMY_TRACK_MODIFICATIONS = False in my application config.
On this page: http://flask-sqlalchemy.pocoo.org/2.3/config/
The following configuration values exist for Flask-SQLAlchemy. Flask-SQLAlchemy loads these values from your main Flask config which can be populated in various ways. Note that some of those cannot be modified after the engine was created so make sure to configure as early as possible and to not modify them at runtime.
In other words, make sure to set up your app.config before creating your Flask-SQLAlchemy database.
For example, if you are configuring your application to set SQLALCHEMY_TRACK_MODIFICATIONS = False:
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Solution 3:[3]
Answer from 2020
If you have questions like this, then you definitely don't need this feature.
By the way, the top answer is outdated. From version 2.1 (released on Oct 23, 2015), this config SQLALCHEMY_TRACK_MODIFICATIONS defaults to None. That's mean the tracking behavior defaults to disabled, you don't need to worry about memory loss.
Unless you are bothered with the terminal warning, you can suppress the warning by setting it to False:
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Solution 4:[4]
Be sure to set your app config properties BEFORE you pass the app to SqlAlchemy. Below is an example of setting up a connection to sql server.
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
SECRET_KEY = os.urandom(32)
SERVER = 'MY_DB_SERVER'
DATABASE = 'MY_DB_NAME'
USERNAME = 'MY_DOMAIN\\MY_USERNAME'
PASSWORD = '$ecretsSecretsarenofun...' # the office ref iykyk
DRIVER = 'SQL SERVER'
DATABASE_CONNECTION = f'MSSQL://{USERNAME}:{PASSWORD}@{SERVER}/{DATABASE}?driver={DRIVER};trusted_connection=yes'
app = Flask(__name__)
# set your config properties BEFORE passing the app to SQLAlchemy
app.config['SECRET_KEY'] = SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_CONNECTION
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Solution 5:[5]
Solved this issue by copy-pasting this onto your shell:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | jasonrhaas |
| Solution 3 | Grey Li |
| Solution 4 | John Nettles |
| Solution 5 | RiveN |
