Elixir + Ecto:不在[数组]中怎么办?
Elixir + Ecto: How to do WHERE NOT IN [array]?
我正在尝试查找在 match_history
字段中没有特定字符串元素的所有 User
。我对此进行了猜测:
matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one
但它似乎在 not
部分中断。有办法吗?
尝试
User |> where([u], not ^device_id in u.match_history)
对于那些正在寻找 "array does not contain any" 行为的人。
例如"match_history does not contain device_1 device_2, device_3"
鉴于您使用的是 PostgreSQL,可以使用带有 array contains @>
operator 的片段。
from t in queryable, where: not fragment("? @> ?::varchar[]", t.match_history, ^device_ids)
或者
from t in queryable, where: fragment("NOT ? @> ?::varchar[]", t.match_history, ^device_ids)
我正在尝试查找在 match_history
字段中没有特定字符串元素的所有 User
。我对此进行了猜测:
matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one
但它似乎在 not
部分中断。有办法吗?
尝试
User |> where([u], not ^device_id in u.match_history)
对于那些正在寻找 "array does not contain any" 行为的人。
例如"match_history does not contain device_1 device_2, device_3"
鉴于您使用的是 PostgreSQL,可以使用带有 array contains @>
operator 的片段。
from t in queryable, where: not fragment("? @> ?::varchar[]", t.match_history, ^device_ids)
或者
from t in queryable, where: fragment("NOT ? @> ?::varchar[]", t.match_history, ^device_ids)