select 映射错误中的 Ecto 子查询
Ecto subquery in select map error
我参考了这个问题 (Ecto Model - subquery in select) 在我的 select 语句中创建了一个子查询,但是我得到了这个错误。
expected a map, got: {%ZB.JournalEntry{meta: #Ecto.Schema.Metadata<:loaded, "journal_entries">
这是我的代码,我是不是遗漏了什么?如果我离开 select 语句代码工作正常。
journal_entries = from entry in JournalEntry,
select: {
entry,
(fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))
},
preload: [
:journal_entry_lines,
journal_entry_lines: :journal_entry,
journal_entry_lines: :chart_account
],
where: entry.id in ^journal_entry_ids and is_nil(entry.deleted_at),
limit: ^per_page,
offset: 0
sort = if not is_nil(params["sort"]) and params["sort"] in JournalEntry.sort_options,
do: String.to_atom(params["sort"]),
else: String.to_atom("date")
direction = if params["direction"] == "asc",
do: :asc,
else: :desc
journal_entries = from entry in journal_entries,
order_by: [{^direction, field(entry, ^sort)}]
render conn, "index.json-api", data: Repo.all(journal_entries), opts: [
include: "journal_entry",
meta: meta_data
]
这是堆栈跟踪,但它似乎没有指出发生错误的行。
web/controllers/journal_entry_controller.ex:1 ZB.JournalEntryController.action/2
web/controllers/journal_entry_controller.ex:1 ZB.JournalEntryController.phoenix_controller_pipeline/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.instrument/4
lib/phoenix/router.ex:261 ZB.Router.dispatch/2
web/router.ex:1 ZB.Router.do_call/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.phoenix_pipeline/1
lib/plug/debugger.ex:123 ZB.Endpoint."call (overridable 3)"/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.call/2
我找到了另一种不会引发错误但不会合并两个对象的方法。
select: %{entry: entry, amount: (fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))},
我可以通过这种方式让它工作
journal_entries = from entry in JournalEntry,
select: %{
entry: entry,
id: entry.id,
account_id: entry.account_id,
archived_at: entry.archived_at,
date: entry.date,
delete_at: entry.deleted_at,
is_closing: entry.is_closing,
is_confirmed: entry.is_confirmed,
note: entry.note,
amount: (fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))
},
我参考了这个问题 (Ecto Model - subquery in select) 在我的 select 语句中创建了一个子查询,但是我得到了这个错误。
expected a map, got: {%ZB.JournalEntry{meta: #Ecto.Schema.Metadata<:loaded, "journal_entries">
这是我的代码,我是不是遗漏了什么?如果我离开 select 语句代码工作正常。
journal_entries = from entry in JournalEntry,
select: {
entry,
(fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))
},
preload: [
:journal_entry_lines,
journal_entry_lines: :journal_entry,
journal_entry_lines: :chart_account
],
where: entry.id in ^journal_entry_ids and is_nil(entry.deleted_at),
limit: ^per_page,
offset: 0
sort = if not is_nil(params["sort"]) and params["sort"] in JournalEntry.sort_options,
do: String.to_atom(params["sort"]),
else: String.to_atom("date")
direction = if params["direction"] == "asc",
do: :asc,
else: :desc
journal_entries = from entry in journal_entries,
order_by: [{^direction, field(entry, ^sort)}]
render conn, "index.json-api", data: Repo.all(journal_entries), opts: [
include: "journal_entry",
meta: meta_data
]
这是堆栈跟踪,但它似乎没有指出发生错误的行。
web/controllers/journal_entry_controller.ex:1 ZB.JournalEntryController.action/2
web/controllers/journal_entry_controller.ex:1 ZB.JournalEntryController.phoenix_controller_pipeline/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.instrument/4
lib/phoenix/router.ex:261 ZB.Router.dispatch/2
web/router.ex:1 ZB.Router.do_call/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.phoenix_pipeline/1
lib/plug/debugger.ex:123 ZB.Endpoint."call (overridable 3)"/2
lib/zipbooks/endpoint.ex:1 ZB.Endpoint.call/2
我找到了另一种不会引发错误但不会合并两个对象的方法。
select: %{entry: entry, amount: (fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))},
我可以通过这种方式让它工作
journal_entries = from entry in JournalEntry,
select: %{
entry: entry,
id: entry.id,
account_id: entry.account_id,
archived_at: entry.archived_at,
date: entry.date,
delete_at: entry.deleted_at,
is_closing: entry.is_closing,
is_confirmed: entry.is_confirmed,
note: entry.note,
amount: (fragment("(SELECT sum(amount) FROM journal_entry_lines WHERE kind = 0 and journal_entry_id = ?)", entry.id))
},