[英]Is There a Way to Fetch a Type Attribute in An Ecto Query
我有一個包含金額字段的付款的 Ecto Schema。 該字段的類型是 ex_money 包中的 Money.Ecto.Composite.Type(v. 5.12.3)。 查詢是:
query =
from(
payment in Payment,
where: payment.amount.currency_code() == ^currency
)
這引發了:
** (Ecto.Query.CompileError) cannot fetch field `currency_code` from `payment.amount`. Can only fetch fields from:
* sources, such as `p` in `from p in Post`
* named bindings, such as `as(:post)` in `from Post, as: :post`
* parent named bindings, such as `parent_as(:post)` in a subquery
有沒有辦法訪問屬性而不讓 Ecto 搜索字段?
這應該工作:
currency = "USD" # atom doesn't work
query =
from(
p in Payment,
where: fragment("(?).amount.currency_code = ?", p, ^currency)
)
請注意,貨幣必須是一個字符串,但如果您有一個原子,當然您總是可以使用to_string
。
這樣做的原因是因為它使用 Postgres 語法來訪問復合類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.