Issue
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_MODIFICATIONS
If 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
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.
Answered By - Jeff Widman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.