Most likely your application doesn't use the Flask-SQLAlchemy event system, so you're probably safe to turn off. You'll need to audit the code to verify--you're looking for anything that hooks into models_committed
or before_models_committed
. If you do find that you're using the Flask-SQLAlchemy event system, you probably should update the code to use SQLAlchemy's built-in event system instead.
The default value as of Flask-SQLAlchemy 2.1 is None
, which is a falsy value, so the event system is disabled. In older versions, the default value was True
, so you'll need to explicitly disable it.
However, in both cases, the warning won't be silenced until this is explicitly set to False
. To do that, add:
SQLALCHEMY_TRACK_MODIFICATIONS = False
to your app config.
Background--here's what the warning is telling you:
Flask-SQLAlchemy has its own event notification system that gets layered on top of SQLAlchemy. To do this, it tracks modifications to the SQLAlchemy session. This takes extra resources, so the option SQLALCHEMY_TRACK_MODIFICATIONS
allows you to disable the modification tracking system.
The rationale for the change is three-fold:
Not many people use Flask-SQLAlchemy's event system, but most people don't realize they can save system resources by disabling it. So a saner default is to disable it and those who want it can turn it on.
The event system in Flask-SQLAlchemy has been rather buggy (see issues linked to in the pull request mentioned below), requiring additional maintenance for a feature 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 a few custom SQLAlchemy event hooks and listeners, and then let SQLAlchemy itself manage the event trigger.
You can see more in the discussion around the pull request that started triggering this warning.
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.
In your code, immediately after:
app = Flask(__name__)
If you want to enable track modifications simply add:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
Otherwise, if you are not using this feature, you may want to change the value to False in order not to waste system resources. This will still silence the warning since you're anyway explicitly setting the config.
Here's the same snippet with False value:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Thanks to Jeff Widman for this added suggestion and details.
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With