sqlalchemy缓存导致Flask REST API的错误响应



我正在使用Flask和SQLAlchemy构建一个rest API。

我遇到了一个问题:如果我使用内部调用SQLAlchemy删除的HTTP delete删除资源,如果我从数据库中重新创建一个元素并尝试使用GET访问该资源,我将收到404 Not Found。

日志显示:2021-10-24 11:37:21,736 INFO sqlalchemy.engine.Engine [cached since [..]s ago] {'isbn_1': '12312', 'param_1': 1}.

看起来,从数据库中获取的方法由于某种原因停留在资源不存在的状态上,即使我在HTTP删除后使用相同的详细信息重新创建它。

这肯定与缓存有关,我不确定如何处理这种情况。

Model - sqlalchemy

from books import Book
from db import Session, engine
local_session = Session(bind=engine)

def get_book_by_isbn(isbn):
"""
Wrapper for an ORM call that is retrieving a book by its ISBN.
:param isbn: isbn code of the book that is to be retrieved
"""
response = OperationResponseWrapper()
try:
response.payload = local_session.query(Book).filter(Book.isbn == isbn).first()
if not response.payload:
response.completed_operation = False
else:
response.completed_operation = True
except Exception as e:
response.erorr = e
response.completed_operation = False
return response

def delete_book_by_isbn(isbn):
"""
Wrapper for an ORM call that is deleting a book by its ISBN.
:param isbn: isbn code of the book that is to be deleted
"""
response = OperationResponseWrapper()
try:
book_to_delete = local_session.query(Book).filter(Book.isbn == isbn).first()
if book_to_delete:
local_session.delete(book_to_delete)
local_session.commit()
else:
response.completed_operation = False
except Exception as e:
response.completed_operation = False
response.erorr = e
return response

Controller - Flask


from model import get_book_by_isbn, delete_book_by_isbn
[..]
@app.route('/api/bookcollection/books/<isbn>', methods=['GET'])
def get_book(isbn):
"""
Method that handles a GET request for a book by the ISBN code.
:param isbn: isbn code of the book that is to be retrieved
"""
db_response = get_book_by_isbn(str(isbn))
body = ''
status = 200
if db_response.erorr:
status = 404
body = ErrorDto(404, str(db_response.erorr), 'EXCEPTION')
elif not db_response.completed_operation:
status = 404
body = ErrorDto(404, 'Requested book does not exist.', 'INVALID_OPERATION')
else:
status = 200
links = LinkDto(request.path, 'books', request.method)
body = BookDto(db_response.payload, links)
return jsonify(body.__dict__), status

@app.route('/api/bookcollection/books/<isbn>', methods=['DELETE'])
def delete_book(isbn):
"""
Method that handles a DELETE request for a book by the ISBN code.
:param isbn: isbn code of the book that is to be deleted
"""
db_response = delete_book_by_isbn(isbn)
body = ''
status = 200
if db_response.erorr:
status = 404
body = ErrorDto(404, str(db_response.erorr), 'EXCEPTION')
elif not db_response.completed_operation:
status = 404
body = ErrorDto(404, 'Requested book does not exist.', 'INVALID_OPERATION')
else:
status = 200
links = LinkDto(request.path, 'books', request.method)
body = StatusDto(200, 'Operation was completed successfully.', links)
return jsonify(body.__dict__), status

好的,似乎我在这个线程中找到了一些解决方案:如何禁用SQLAlchemy缓存?

engine = create_engine(connection_string, echo=True, isolation_level="READ UNCOMMITTED")

添加isolation_level="READ UNCOMMITTED"似乎有帮助,但我不确定这个动作有什么其他含义。

相关内容

最新更新