我正在尝试使用Absinthe#Batch进行一个简单的查询,但是执行Ecto查询的函数不会返回任何结果。
我添加了一些跟踪来计算数据库上的记录。预期总是返回1,但不返回。
救世主:
def get_cards_for_stage(stage, _args, _ctx) do
IO.puts "Before batch: #{OfferSid.Repo.aggregate(from(p in "pipeline_cards"), :count, :id)}"
batch({__MODULE__, :pipeline_cards_by_stage_ids}, stage.id, fn batch_results ->
IO.puts "Inside batch: #{OfferSid.Repo.aggregate(from(p in "pipeline_cards"), :count, :id)}"
{:ok, Map.get(batch_results, stage.id)}
end)
end帮助方法:
def pipeline_cards_by_stage_ids(_args, stages_ids) do
IO.puts "On the helper method: #{OfferSid.Repo.aggregate(Ecto.Query.from(p in "pipeline_cards"), :count, :id)}"
cards = OfferSid.pipeline_cards_by_stage_ids(stages_ids)
Map.new(cards, fn s -> {s.pipeline_stage_id, s} end)
end一些追踪:
# Before batch: 1
# Before batch: 1
# Before batch: 1
# On the helper method: 0
# Inside batch: 1
# Inside batch: 1
# Inside batch: 1发布于 2018-01-09 18:27:50
正如benwilson512提到的这里,问题在于db连接不是共享。
所以,我修正了它,并补充道:
before do
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
endhttps://stackoverflow.com/questions/48158729
复制相似问题