Elixir + Ecto: How to do WHERE NOT IN [array]?

帅比萌擦擦* 提交于 2020-01-03 08:09:01

问题


I am trying to look for all Users that don't have a certain string element in their match_history field. I took a guess with this:

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

But it seems to break at the not part. Is there a way to do this?


回答1:


Try

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




回答2:


For those who are looking for a "array does not contain any" behaviour.
For example "match_history does not contain device_1 device_2, device_3"

Given you're using PostgreSQL, can use a fragment with the array contains @> operator.

from t in queryable, where: not fragment("? @> ?::varchar[]", t.match_history, ^device_ids)

Or

from t in queryable, where: fragment("NOT ? @> ?::varchar[]", t.match_history, ^device_ids)


来源:https://stackoverflow.com/questions/41005307/elixir-ecto-how-to-do-where-not-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!