当查询在编译时未知但知道函数何时为 运行 时,如何避免 'unbound variable' 错误?
How do I avoid an 'unbound variable' error when the query is unknown at compile but known when the function would be run?
我目前有一个查询正在进行以下旅程:
# Context
def list_customers(params) do
items =
from(i in MyAppItems)
|> MyappItems.filter_by_params(params |> Enum.to_list())
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id])
|> join(:left, [_, i], pr in assoc(i, :provider))
|> join(:left, [_, i, _], t in assoc(i, :type))
|> join(:left, [_, i, _, _], s in assoc(i, :status))
|> join(:left, [_, i, _, _, _], a in assoc(i, :action))
|> join(:left, [_, i, _, _, _, _], n in assoc(i, :note))
|> preload([_, i, pr, t, s, a, n],
items: {i, provider: pr, type: t, status: s, action: a, note: n}
)
|> group_by([p, _, _, _, _, _, _], p.id)
|> Repo.all()
end
# MyAppItems
def filter_by_params(query, params) do
Enum.reduce(params, query, fn
{"list_date", list_date}, query ->
filter_by_list_date(query, list_date)
_, query ->
query
end)
end
defp filter_by_list_date(query, list_date) do
{:ok, date} = Date.from_iso8601(list_date)
query
|> where(fragment("date(inserted_at) = ?", ^date))
end
照原样,当它运行时,我收到关于 inserted_at
.
的 ambiguous column
警告
我尝试通过如下更改片段来解决此问题:
|> where(fragment("date(?) = ?", i.inserted_at, ^date))
但是我无法摆脱围绕 i.
的 unbound_variable
错误。我知道当查询运行时 i
将在传递给片段的查询中,但由于编译错误我无法到达那一点。
您可以为您的联接添加别名以在以后的管道链中引用。
例如:
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id], as: :item)
|> where([item: item], fragment("date(?) =?", item.inserted_at, ^date))
或者,如果您知道硬编码联接,则可以执行与联接相同的操作
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id], as: :item)
|> where([_, i], fragment("date(?) =?", i.inserted_at, ^date))
我目前有一个查询正在进行以下旅程:
# Context
def list_customers(params) do
items =
from(i in MyAppItems)
|> MyappItems.filter_by_params(params |> Enum.to_list())
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id])
|> join(:left, [_, i], pr in assoc(i, :provider))
|> join(:left, [_, i, _], t in assoc(i, :type))
|> join(:left, [_, i, _, _], s in assoc(i, :status))
|> join(:left, [_, i, _, _, _], a in assoc(i, :action))
|> join(:left, [_, i, _, _, _, _], n in assoc(i, :note))
|> preload([_, i, pr, t, s, a, n],
items: {i, provider: pr, type: t, status: s, action: a, note: n}
)
|> group_by([p, _, _, _, _, _, _], p.id)
|> Repo.all()
end
# MyAppItems
def filter_by_params(query, params) do
Enum.reduce(params, query, fn
{"list_date", list_date}, query ->
filter_by_list_date(query, list_date)
_, query ->
query
end)
end
defp filter_by_list_date(query, list_date) do
{:ok, date} = Date.from_iso8601(list_date)
query
|> where(fragment("date(inserted_at) = ?", ^date))
end
照原样,当它运行时,我收到关于 inserted_at
.
ambiguous column
警告
我尝试通过如下更改片段来解决此问题:
|> where(fragment("date(?) = ?", i.inserted_at, ^date))
但是我无法摆脱围绕 i.
的 unbound_variable
错误。我知道当查询运行时 i
将在传递给片段的查询中,但由于编译错误我无法到达那一点。
您可以为您的联接添加别名以在以后的管道链中引用。
例如:
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id], as: :item)
|> where([item: item], fragment("date(?) =?", item.inserted_at, ^date))
或者,如果您知道硬编码联接,则可以执行与联接相同的操作
MyAppCustomer
|> join(:left, [p], i in ^items, on: [customer_id: p.id], as: :item)
|> where([_, i], fragment("date(?) =?", i.inserted_at, ^date))