欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppres

程序员文章站 2022-03-25 21:54:39
错误信息如下: 错误信息提示的很明确,修改 SQLALCHEMY_TRACK_MODIFICATIONS 为True以移除这个警告。 去flask/lib/python2.7/site-packages/flask_sqlalchemy的init.py里面修改吧。 在init.py里面有init_a ......
在同一个项目中由于flask_sqlalchemy版本不同,有时会报如下错误

错误信息如下:

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.')

错误信息提示的很明确,修改 sqlalchemy_track_modifications 为true以移除这个警告。

 

去flask/lib/python2.7/site-packages/flask_sqlalchemy的init.py里面修改吧。

Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppres

 

 


在init.py里面有init_app方法,修改下面的一行:

track_modifications = app.config.setdefault['sqlalchemy_track_modifications', true]

然后保存,运行。

Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppres

 

注意

在后面程序中仍然出现错误

>>> from app import db,models
>>> u = models.user(nickname='john', email='john@email.com')
>>> db.session.add(u)
traceback (most recent call last):
  file "<stdin>", line 1, in <module>
  file "/users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  file "/users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 1012, in __call__
    return self.registry.setdefault(key, self.createfunc())
  file "/users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 3214, in __call__
    return self.class_(**local_kw)
  file "/users/simufengyun/microblog/flask/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 137, in __init__
    #track_modifications = app.config['sqlalchemy_track_modifications']
keyerror: ('sqlalchemy_track_modifications', true)

图示如下

Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppres

 

 

请参考如下文章进行修改: 

关于keyerror的错误:'sqlalchemy_track_modifications'。

# __init__.py
class signallingsession(sessionbase):

    def __init__(self, db, autocommit=false, autoflush=true, **options):
        #: the application that this session belongs to.
        self.app = app = db.get_app()
        track_modifications = app.config['sqlalchemy_track_modifications']
        bind = options.pop('bind', none) or db.engine
        binds = options.pop('binds', db.get_binds(app))

        if track_modifications is none or track_modifications:
            _sessionsignalevents.register(self)

        sessionbase.__init__(
            self, autocommit=autocommit, autoflush=autoflush,
            bind=bind, binds=binds, **options
        )
关键字“sqlalchemy_track_modifications”不在app.config中,app.config在使用前未初始化。所以我建议稍微改变如下:

刚刚在“self.session = self.create_scoped_session(session_options)”之前初始化了“self.app = app”。
# __init__.py
class sqlalchemy(object):
    """this class is used to control the sqlalchemy integration to one
    or more flask applications.  depending on how you initialize the
    object it is usable right away or will attach as needed to a
    flask application.
    ... ... ...
    .. versionchanged:: 3.0
       utilise the same query class across `session`, `model.query` and `query`.
    """

    #: default query class used by :attr:`model.query` and other queries.
    #: customize this by passing ``query_class`` to :func:`sqlalchemy`.
    #: defaults to :class:`basequery`.
    query = none

    def __init__(self, app=none, use_native_unicode=true, session_options=none,
                 metadata=none, query_class=basequery, model_class=model):
        self.app = app
        self.use_native_unicode = use_native_unicode
        self.query = query_class
        self.session = self.create_scoped_session(session_options)
        self.model = self.make_declarative_base(model_class, metadata)
        self._engine_lock = lock()
        # self.app = app

        _include_sqlalchemy(self, query_class)

        if app is not none:
            self.init_app(app)
添加代码:
        if self.app is not none:
            return self.app
代码之前:

        if current_app:
            return current_app._get_current_object()
如下:

# __init__.py

    def get_app(self, reference_app=none):
        """helper method that implements the logic to look up an
        application."""

        if reference_app is not none:
            return reference_app

        if self.app is not none:
            return self.app

        if current_app:
            return current_app._get_current_object()

        # if self.app is not none:
        #   return self.app

        raise runtimeerror(
            'no application found. either work inside a view function or push'
            ' an application context. see'
            ' http://flask-sqlalchemy.pocoo.org/contexts/.'
        )
这样,“self.app = app = db.get_app()”将返回实例参数“app”通过“sqlalchemy(app)”传输,而不是引发错误。

因为,代码中的“self.app”

        if self.app is not none:
            return self.app
不会是none,也不会出错。

现在我们可以在使用“db = sqlalchemy(app)”作为下面的代码之前初始化“app.config”:

#coding:utf8
import pymysql
from flask_sqlalchemy import sqlalchemy
from flask import flask

app = flask(__name__)

app.config["sqlalchemy_database_uri"] = "mysql+pymysql://root:whm@47.x.x.x:3306/artcs_pro"
app.config["sqlalchemy_track_modifications"] = true

db = sqlalchemy(app)

  注意以上所有修改操作都要重新启动 flask/bin/python