Lar*_*uph 2 python sqlalchemy python-3.x flask-sqlalchemy
因此,我制作了一个个人网络服务器来抓取一些网站并将数据显示在网页上。但是,当我尝试将结果提交到数据库时,我提交的每个视频都会收到警告。我想知道这个警告是否可以修复或至少隐藏?
这是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
这是完整的警告输出:
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?
Run Code Online (Sandbox Code Playgroud)
sna*_*erb 11
发出此警告的原因是:
中的对象old_db仍然是原始对象,因此您可以选择忽略警告,但通常最好在警告导致错误之前对其进行处理(这就是它们是警告的原因)。
您可以采取以下一项或全部步骤:
* 或删除(如果是scoped_session.
| 归档时间: |
|
| 查看次数: |
4238 次 |
| 最近记录: |