尝试使用Ecto查询进行简单的操作,但部分位置转换为TRUE

时间:2016-02-21 01:20:51

标签: phoenix-framework ecto

def all_for_me(query, user_id) do
    friends = Repo.all(from f in Friendship, where: f.user_id == ^user_id and f.status == 2, select: f.friend_id)

    from p in query,
      where: p.privacy == "public" or (p.privacy == "friends" and p.user_id in ^friends),
    select: p,
    order_by: [desc: p.inserted_at]
  end

OR或(p.privacy ==“friends”和 p.user_id in ^ friends )部分即将出现((p0。“privacy”='friends')AND ))

我错过了什么?我知道它已经很晚了,我可能已经使用了太长时间,但需要在睡觉前解决这个问题!

1 个答案:

答案 0 :(得分:0)

最有可能的是,friends是一个空列表。在下面的REPL会话中,x in ^[]在原始SQL中被有效地转换为false

iex(7)> TestProject.User |> where([x], x.id in ^[Ecto.UUID.generate])  |> Repo.all
[debug] SELECT u0."id", u0."name", u0."email", u0."password_hash", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" IN ($1)) [<<98, 168, 63, 177, 44, 237, 76, 176, 148, 149, 173, 48, 68, 96, 235, 60>>] OK query=1.2ms
[]
iex(8)> TestProject.User |> where([x], x.id in ^[])  |> Repo.all                  
[debug] SELECT u0."id", u0."name", u0."email", u0."password_hash", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (false) [] OK query=1.1ms
[]