Elixir 声明层有问题;我想为模型的每个实例检索特定列的数据,如下所示:
File.query.values("column")
问题是,它仅适用于过滤查询,示例的简单查询失败并出现以下错误:
Could not locate a bind configured on SQL expression or this Session
这种看起来像 Elixir 中的错误,但我找不到解决方法,也许我完全忽略了它应该工作的方式。
Elixir 声明层有问题;我想为模型的每个实例检索特定列的数据,如下所示:
File.query.values("column")
问题是,它仅适用于过滤查询,示例的简单查询失败并出现以下错误:
Could not locate a bind configured on SQL expression or this Session
这种看起来像 Elixir 中的错误,但我找不到解决方法,也许我完全忽略了它应该工作的方式。
使用表格
File.query.values(Table.column)
它应该可以工作。它返回一个生成器,所以用 list() 包装来获取一个序列。请参阅此示例交互式输出:
>>> User.query.values('display_name')
Traceback (most recent call last):
File "<console>", line 1, in ?
[snip traceback]
UnboundExecutionError: Could not locate a bind [ ... ] or this Session
>>> User.query.values(User.display_name).next()
(u'Vinay Sajip',)
顺便说一句,这并不是 Elixir 的问题——从 Elixir 实体的查询属性返回的查询是标准的 SQLAlchemy 查询对象。注意 Query.values() 的 SQLAlchemy 文档:
返回一个迭代器,产生对应于给定列列表的结果元组