【问题标题】:Getting Record from Postgres using Ecto Elixir使用 Ecto Elixir 从 Postgres 获取记录
【发布时间】:2022-01-11 12:29:01
【问题描述】:

我正在尝试通过电子邮件吸引用户。获取合并函数是不存在的,甚至我没有写合并函数。

代码

def get_user_by_email(email) do
    User
    |> preload(:exchange_accounts)
    |> preload(:pos_account)
    |> Repo.all(from u in User, where: u.email == ^email)
  end

错误

iex(58)> AAK.POS.HelperFunction.get_user_by_email("sohaibanwaar36@gmail.com")
** (FunctionClauseError) no function clause matching in Keyword.merge/2    
    
    The following arguments were given to Keyword.merge/2:
    
        # 1
        []
    
        # 2 
        #Ecto.Query<from u0 in AAK.Accounts.User,
         where: u0.email == ^"sohaibanwaar36@gmail.com">
    
    Attempted function clauses (showing 3 out of 3):
    
        def merge(keywords1, []) when is_list(keywords1)
        def merge([], keywords2) when is_list(keywords2)
        def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) 
    
    (elixir 1.11.1) lib/keyword.ex:764: Keyword.merge/2
    (aak 0.1.0) lib/aak/repo.ex:4: AAK.Repo.all/2

【问题讨论】:

    标签: postgresql elixir phoenix-framework ecto


    【解决方案1】:

    应该将Ecto.Query.preload/3 用于查询, Ecto.Repo.preload/3 用于结构,但不能混合使用。

    下面是带有查询的变体。

    def get_user_by_email(email) do
      Repo.all(
        from u in User,
        where: u.email == ^email,
        preload: ~w|exchange_accounts pos_account|a
      )
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多