药剂+外生:如何做WHERE NOT IN [阵列]?

问题描述 投票:10回答:2

我试图寻找那些没有在他们的User领域的某些字符串元素的所有match_historys。我花了猜测与此:

matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one

但似乎在not部分断裂。有没有办法做到这一点?

elixir ecto
2个回答
12
投票

尝试

User |> where([u], not ^device_id in u.match_history)


1
投票

对于那些谁正在寻找一个“阵列不包含任何”行为。 例如,“match_history不含DEVICE_1设备_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)
© www.soinside.com 2019 - 2024. All rights reserved.