首页 > 解决方案 > Flask-SQLAlchemy:SAWarning:身份映射已经有(对象)的身份,用新刷新的对象替换它

问题描述

所以我制作了一个个人网络服务器来抓取一些网站并在网页上显示数据。但是当我尝试将结果提交到数据库时,我收到了我提交的每个视频的警告。我想知道这个警告是否可以修复或至少隐藏?

这是我的代码:

from web_scraper import db
from web_scraper.models import Video

old_db = Video.query.all() # existing database to compare with the new one
db_queue = list() # scraping results ready to be added to the database

### web scraping occurs here to fill db_queue

db.drop_all() # reset db
db.create_all() # recreate a new one
for vid in db_queue:
    db.session.add(vid) # adding all pending results
db.session.commit() # error thrown here

这是完整的警告输出:

C:\DevApps\Python38\lib\site-packages\sqlalchemy\orm\session.py:1948: SAWarning: Identity map already had
an identity for (<class 'web_scraper.models.Video'>, (2133,), None), replacing it with newly flushed object.
Are there load operations occurring inside of an event handler within the flush?

标签: pythonpython-3.xsqlalchemyflask-sqlalchemy

解决方案


发出此警告是因为:

  • 创建了一个会话
  • 现有对象被提取到会话中
  • 数据库表被删除并重新创建
  • 新对象被添加到同一会话中
  • 新对象与添加到原始会话的对象具有相同的 id,因此 SQLAlchemy 警告会话中的原始对象正在被新对象覆盖

中的对象old_db仍然是原始对象,因此您可以选择忽略警告,但通常最好在警告导致错误之前处理它们(这就是警告的原因)。

您可以采取以下一个或两个步骤:

  • 在添加新对象之前关闭*原始会话并打开一个新会话
  • 而不是删除和重新创建表,删除它们包含的对象

* 或者remove如果它是scoped_session.


推荐阅读