Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
在同一个项目中由于flask_sqlalchemy版本不同,有时会报如下错误
错误信息如下:
flask_sqlalchemy\__init__.py:: 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.com')
>>> db.session.add(u)
Traceback (most recent call last):
File "<stdin>", line , in <module>
File "/Users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line , in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line , in __call__
return self.registry.setdefault(key, self.createfunc())
File "/Users/simufengyun/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line , in __call__
return self.class_(**local_kw)
File "/Users/simufengyun/microblog/flask/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line , in __init__
#track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: ('SQLALCHEMY_TRACK_MODIFICATIONS', True)
图示如下
请参考如下文章进行修改:https://github.com/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.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
Flask报如下错误:SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.的更多相关文章
- 解决'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
懒癌晚期,直接贴图 然后就解决了!
- SQLALCHEMY_TRACK_MODIFICATIONS adds significant异常的解决方法
- LR报-27727错误解决办法
1.报如下错误:Action.c(4):Error-27727:Step download timeout(120 seconds) has expired when downloading reso ...
- Flask報錯 KeyError 'SQLALCHEMY_TRACK_MODIFICATIONS'.md
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS' 关于flask-sqlalchemy中出现的 KeyError: 'SQLALCHEMY_TRACK_MODIFI ...
- Python Flask UnicodeDecodeError 编码错误解决
折腾Python做快速Web开发.最后定下来用Flask,相对教程全面. utf8编码上遇到问题,所有文件已经是utf8编码保存,加载css.js等静态文件,如果用GBK编码就正常:用utf8就报Un ...
- WinServer2008R2 + IIS 7.5 + .NET4.0 经典模式 运行WebAPI程序报404错误的解决方案
在Windows Server 2008 R2系统下,IIS 7.5 + .NET Framework 4.0的运行环境,以经典模式(Classic Mode)部署一个用.NET 4.0编译的 Web ...
- 启动mysql服务 报1067 错误
启动mysql 报1067 错误 一般报1067错误,先看一下data/my.ini配置文件 中的路径 datadir ,log-bin ,log-error 报1067错误原因 多种 ...
- MVC中使用jquery uploadify上传图片报302错误
使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误.原来更新了fl ...
- 开机报这个错误,然后 adobe软件无法使用
开机报这个错误,然后 adobe软件无法使用 按照此方法测试,无效,在原基础如下更改 /tmp 是连接文件找到根文件(/private/tmp)删除重新给予权限,重新建立连接恢复正常 使用到的命令 ...
随机推荐
- 管网平差的python程序
在市政给水管网当中,管网平差的目的是在已知节点流量.管段长度的情况下,求得各管段流量和对应的经济管径.本科生学习阶段了解并掌握管网平差原理及方法是必不可少的环节. 在下面的程序当中,将利用哈代克罗斯法 ...
- React的setState学习及应用
React的setState学习及应用 一:作用: setState() 将对组件 state 的更改排入队列,并通知 React 需要使用更新后的 state 重新渲染此组件及其子组件.这是用于更新 ...
- 物联网架构_对AWS的Greengrass的认识与理解
物联网架构_对AWS的Greengrass的认识与理解 一,前言: 这段时间有许多的收获,分析,还有总结,其中包括新系统的设计与开发,以及其中新技术的踩坑等等等. 但是最近真的很忙,项目的推进,面试工 ...
- mysql 排它锁之行锁、间隙锁、后码锁
MySQL InnoDB支持三种行锁定 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock):锁定索引记录间隙,确保索引记录的间隙不变.间隙锁是针对事务 ...
- python高阶函数——sorted排序算法
python 内置的sorted()函数可以对一个list进行排序: >>> sorted([8,3,8,11,-2]) [-2, 3, 8, 8, 11] 既然说是高阶函数,那么它 ...
- JPA中实现单向一对多的关联关系
场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...
- 【Kafka】《Kafka权威指南》——提交和偏移量
KafkaConsumer(消费者)每次调用 poll()方法,它总是返回由生产者写入 Kafka但还没有被消费者读取过的记录, 我们因 此可以追踪到哪些记录是被群组里的哪个消费者读取的.之前已经讨论 ...
- Linux常用命令及详细说明 — 结合工作(侧重性能监控,包括CPU、内存、IO、网络、磁盘等)
(一)Linux监控的几个常用命令(对于服务器后端程序猿很重要,必须掌握): 命令 功能 命令 功能 iostat 统计CPU及网络.设备和分区IO的数据 vmstat 展示给定时间服务器的状态值(包 ...
- 并发编程~~~协程~~~greenlet模块, gevent模块
一 协程 1. 协程: 单线程下的并发,又称微线程,纤程.协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 并发真正的核心: 切换并且保持状态. 开启协程并发的执行,自己的程序把控着C ...
- 12. Go 语言文件处理
Go 语言文件处理 本章我们将带领大家深入了解一下 Go语言中的文件处理,重点在于文件而非目录或者通用的文件系统,特别是如何读写标准格式(如 XML 和 JSON 格式)的文件以及自定义的纯文本和二进 ...