admin管理员组文章数量:1130349
在同一个项目中由于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里面修改吧。
在init.py里面有init_app方法,修改下面的一行:
track_modifications = app.config.setdefault['SQLALCHEMY_TRACK_MODIFICATIONS', True]
然后保存,运行。
注意
在后面程序中仍然出现错误
>>> from app import db,models
>>> u = models.User(nickname='john', email='john@email')
>>> 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)
图示如下
请参考如下文章进行修改:https://github/pallets/flask-sqlalchemy/issues/609
关于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/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
在同一个项目中由于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里面修改吧。
在init.py里面有init_app方法,修改下面的一行:
track_modifications = app.config.setdefault['SQLALCHEMY_TRACK_MODIFICATIONS', True]
然后保存,运行。
注意
在后面程序中仍然出现错误
>>> from app import db,models
>>> u = models.User(nickname='john', email='john@email')
>>> 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)
图示如下
请参考如下文章进行修改:https://github/pallets/flask-sqlalchemy/issues/609
关于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/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
本文标签: 错误addsSQLALCHEMYTRACKMODIFICATIONSflaskdisabled
版权声明:本文标题:Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default ... 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1754938406a2743874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论