0

我正在尝试使用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
4

1 回答 1

1

正如这里提到的benwilson512,问题是 db 连接没有共享

所以,我修复了它添加:

before do
  Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
于 2018-01-09T18:27:50.653 回答