gpt4 book ai didi

elixir - 带有多个 'or' 子句的 Ecto Repo.get_by

转载 作者:行者123 更新时间:2023-12-02 09:11:08 25 4
gpt4 key购买 nike

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

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

最佳答案

Repo.get_by不允许 OR中的条款。您需要使用 from 编写完整的查询。 ,或 where 的组合和 or_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 .

关于elixir - 带有多个 'or' 子句的 Ecto Repo.get_by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com