小编apo*_*kul的帖子

SQLAlchemy批量插入失败

我在循环中使用SQLAlchemy进行批量插入,如下所示:

for table, batch in pendingInserts:
    self.conn.execute(table.insert(), batch)
Run Code Online (Sandbox Code Playgroud)

这里batch是名单dicttable是SQLAlchemy的表.第一批插入成功执行但在后续迭代中,同一表上的insert失败,并出现以下错误:

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 .....

security_exchange是DB(PostgreSQL)中的可空列,因此它不是必需的,并且在批处理中的所有条目中都被省略.我很困惑为什么它成功第一次批量插入但在同一个表上第二次插入失败.同样对于同一个表,对于所有批次中的所有dicts,提供的列数始终保持相同.

python sqlalchemy

3
推荐指数
1
解决办法
1378
查看次数

在 SQLAlchemy Core 中从多表连接中选择列

我在 SQLAlchemy Core 中加入 3 个表并选择所有列,如下所示:

rows = self.db.execute(self.execs.join(
                         self.orders.join(self.instruments)
                      ).select(whereClause)).reduce_columns())
Run Code Online (Sandbox Code Playgroud)

它运行良好,但如果我想选择列的子集:

reqdCols = [order.c.id, exec.c.last_modified, instruments.type]
rows = self.db.execute(self.execs.join(
                         self.orders.join(self.instruments)
                      ).select(reqdCols, whereClause)).reduce_columns())
Run Code Online (Sandbox Code Playgroud)

它不起作用并出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 807, in select
    return Select(collist, whereclause, from_obj=[self], **kwargs)
  File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 2219, in __init__
    whereclause).self_group(against=operators._asbool)
  File "/apps/qtdist/pkgs/pub/cpython/python-2.7.6/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 3438, in _literal_as_text
    "SQL expression object or string expected."
sqlalchemy.exc.ArgumentError: SQL expression object or string expected.
Run Code Online (Sandbox Code Playgroud)

替代方法是使用 select 而不是 Join.select 并使其与 where 子句隐式连接:

joinConditions …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy

3
推荐指数
1
解决办法
2642
查看次数

突变后自动更新 apollo 客户端缓存,不影响现有查询

我有一个突变(UploadTransaction)返回某个名为 Transaction 的对象的特定列表。

#import "TransactionFields.gql" 
mutation UploadTransaction($files: [Upload!]!) {
  uploadFile(files: $files){
    transactions {
      ...TransactionFields
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

从后端(石墨烯)返回的交易具有 id 和 typename 字段。因此它应该自动更新缓存中的事务。在 Apollo 的 chrome 开发工具中,我可以看到新的交易:

在此处输入图片说明

我还有一个查询 GetTransactions 获取所有 Transaction 对象。

#import "TransactionFields.gql"
query GetTransactions {
  transactions {
    ...TransactionFields
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我没有看到查询返回新添加的事务。在初始加载期间,Apollo 客户端加载了 292 个事务,显示在 ROOT_QUERY 下。它不断返回相同的 292 笔交易。UploadTransaction 突变在开发工具的缓存中添加“事务”类型的新对象,而不会影响开发工具中的 ROOT_QUERY 或我在代码中的查询。

在此处输入图片说明

TransactionFields.gql 是

fragment TransactionFields on Transaction {
    id
    timestamp
    description
    amount
    category {
      id
      name
    }
    currency
}
Run Code Online (Sandbox Code Playgroud)

知道我做错了什么吗?我是阿波罗客户端和 graphql 的新手

graphql react-apollo apollo-client

1
推荐指数
1
解决办法
2434
查看次数