简体   繁体   English

SQLAlchemy批量插入失败

[英]SQLAlchemy bulk insert failing

I am doing bulk inserts using SQLAlchemy in a loop as follows: 我在循环中使用SQLAlchemy进行批量插入,如下所示:

for table, batch in pendingInserts:
    self.conn.execute(table.insert(), batch)

where batch is list of dict and table is a SQLAlchemy table. 其中batchdict列表, table是SQLAlchemy表。 First batch of inserts executes successfully but in subsequent iteration with insert on same table fails with the following error: 第一批插入成功执行,但在随后的迭代中,同一表上的插入失败,并出现以下错误:

StatementError: A value is required for bind parameter 'security_exchange', in parameter group 45 (original cause: InvalidRequestError: A value is required for bind parameter 'security_exchange', in parameter group 45) u'INSERT INTO .....

Here security_exchange is nullable column in the DB (PostgreSQL), so it in not mandatory and is omitted in all entries in the batch. 这里的security_exchange是数据库(PostgreSQL)中的可空列,因此它不是强制性的,并且在批处理中的所有条目中都将其省略。 I am confused why it succeed for the first bulk insert but fails for the second insert on same table. 我很困惑,为什么它对同一表的第一个大容量插入成功而对第二个插入失败。 Also for the same table, the number of columns supplied always remains same for all dicts in all batches. 同样对于同一张表,所有批次中的所有字典提供的列数始终保持不变。

Batch insert needs all items to have same set of columns present for all items in the batch. 批处理插入需要所有项目在批处理中的所有项目中都具有相同的列集。 In my case some of the optional columns (with default values) were getting skipped for some of the items in the batch. 在我的情况下,批处理中的某些项目跳过了一些可选列(具有默认值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM