【问题标题】:Try to do a simple where in with an Ecto query, but the where in part is translating to TRUE尝试使用 Ecto 查询做一个简单的 where in,但 where 部分转换为 TRUE
【发布时间】:2016-02-25 08:00:08
【问题描述】:
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" and p.user_id in ^friends) 部分正在输出为 ((p0."privacy" = 'friends') AND ))

我错过了什么?我知道现在已经很晚了,我可能已经玩了太久了,但需要在睡觉前解决这个问题!

【问题讨论】:

    标签: phoenix-framework ecto


    【解决方案1】:

    很可能,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
    []
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多