[英]Bulk INSERT IGNORE using Flask-SQLAlchemy
I'm trying to update a database using API-gathered data, and I need to make sure all tables are being updated.我正在尝试使用 API 收集的数据更新数据库,我需要确保所有表都被更新。
Sometime I will receive data that's already in the database, so I want to do an INSERT IGNORE.有时我会收到数据库中已有的数据,所以我想做一个 INSERT IGNORE。
My current code is something like this:我目前的代码是这样的:
def update_orders(new_orders):
entries = []
for each_order in new_orders:
shipping_id = each_order['id']
title = each_order['title']
price = each_order['price']
code = each_order['code']
source = each_order['source']
phone = each_order['phone']
category = each_order['delivery_category']
carrier = each_order['carrier_identifier']
new_entry = Orders(
id=shipping_id,
title=title,
code=code,
source=source,
phone=phone,
category=category,
carrier=carrier,
price=price
)
entries.append(new_entry)
if len(entries) == 0:
print('No new orders.')
break
else:
print('New orders:', len(entries))
db.session.add_all(entries)
db.session.commit()
This works well when I'm creating the database from scratch, but it will give me an error if there's duplicate data, and I'm not able to commit the inserts.当我从头开始创建数据库时,这很有效,但如果有重复数据,它会给我一个错误,并且我无法提交插入。
I've been reading for a while, and found a workaround that uses prefix_with
:我已经阅读了一段时间,并找到了一种使用
prefix_with
的解决方法:
print('New orders:', len(entries))
if len(entries) == 0:
print('No new orders.')
else:
insert_command = Orders.__table__.insert().prefix_with('OR IGNORE').values(entries)
db.session.execute(insert_command)
db.session.commit()
The problem is that values(entries)
is a bunch of objects: <shop.database.models.Orders object at 0x11986def0>
instead of being the instance of the class, is the class instance object in memory.问题是
values(entries)
是一堆对象: <shop.database.models.Orders object at 0x11986def0>
而不是类的实例,而是内存中的类实例对象。
Anybody has any suggestion on approaching this problem?有人对解决这个问题有什么建议吗? Feel free to suggest a different approach, or just an adjustment.
随意提出不同的方法,或者只是一个调整。
Thanks a lot.非常感谢。
What database are you using ?你用的是什么数据库? Under MySQL, "INSERT OR IGNORE" is not valid syntax, instead one should use "INSERT IGNORE".
在 MySQL 下,“INSERT OR IGNORE”不是有效的语法,而应该使用“INSERT IGNORE”。 I had the same situation and got my query to work with the following:
我遇到了同样的情况,并让我的查询使用以下内容:
insert_command = Orders.__table__.insert().prefix_with(' IGNORE').values(entries)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.