1 出现错误FSADeprecationWarning
完整错误描述为:
FSADeprecationWarning: 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 '
解决办法👇
对于yi'you代码段:
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqldb://root:******@localhost/test" # 涂掉的部分是自己的mysql登录密码
db = SQLAlchemy(app)
增加语句 app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True # 位置必须在db = SQLAlchemy(app) 之前
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqldb://root:******@localhost/test" # 涂掉的部分是自己的mysql登录密码
db = SQLAlchemy(app)
2 运行代码时不报错,但在操作网页连接数据库时报错
错误代码反馈
raceback (most recent call last):
File "D:\Anaconda3\envs\python36\lib\site-packages\sqlalchemy\util\_collections.py", line 1010, in __call__
return self.registry[key]
KeyError: 22180
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "D:\Anaconda3\envs\python36\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:/python_study_files/flask_study/8_files(更新7)/app.py", line 27, in login
if u.isExisted(): # 调用model.py文件中的isExisted函数
File "F:\python_study_files\flask_study\8_files(更新7)\model.py", line 42, in isExisted
temUser = user.query.filter_by(username=self.username, password=self.password).first()
File "D:\Anaconda3\envs\python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 519, in __get__
return type.query_class(mapper, session=self.sa.session())
File "D:\Anaconda3\envs\python36\lib\site-packages\sqlalchemy\orm\scoping.py", line 78, in __call__
return self.registry()
File "D:\Anaconda3\envs\python36\lib\site-packages\sqlalchemy\util\_collections.py", line 1012, in __call__
return self.registry.setdefault(key, self.createfunc())
File "D:\Anaconda3\envs\python36\lib\site-packages\sqlalchemy\orm\session.py", line 3225, in __call__
return self.class_(**local_kw)
File "D:\Anaconda3\envs\python36\lib\site-packages\flask_sqlalchemy\__init__.py", line 137, in __init__
track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'
解决办法,参考https://blog.csdn.net/qq_41717584/article/details/83278268
注释掉app.py文件中的以下语句(与model文件中的该语句重复)
app = Flask(__name__)