【问题标题】:Ecto Repo.get_by with multple 'or' clauses带有多个“或”子句的 Ecto Repo.get_by
【发布时间】:2018-08-31 11:29:09
【问题描述】:

我正在尝试将 Ecto Repo.get_by 与多个子句一起使用,但我无法找到语法示例。我需要查询数据库中的一个或两个字段匹配的位置,因此子句之间存在“或”条件。我不知道这是否可能。

Repo.get_by(User, [username: username, email: username], prefix: :accounts) 

【问题讨论】:

    标签: elixir ecto


    【解决方案1】:

    Repo.get_by 不允许使用 OR 子句。您需要使用fromwhereor_where 的组合编写完整的查询,然后通过管道传输到Repo.one

    from(u in User, where: u.username == ^username or u.email == ^username)
    |> Repo.one
    
    User
    |> where(username: ^username)
    |> or_where(email: ^username)
    |> Repo.one
    

    要为该查询添加前缀,您可以使用this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多