How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

前端 未结 5 479
忘了有多久
忘了有多久 2020-12-12 12:26

Every time I run my app that uses Flask-SQLAlchemy I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS option will be disabled.

         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 12:50

    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.

提交回复
热议问题