Home > Net >  Getting Record from Postgres using Ecto Elixir
Getting Record from Postgres using Ecto Elixir

Time:12-07

I am trying to get users by email. Getting merge function does not exist, even I didn't write the merge function.

Code

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

Error

iex(58)> AAK.POS.HelperFunction.get_user_by_email("[email protected]")
** (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 == ^"[email protected]">
    
    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

CodePudding user response:

One should either use Ecto.Query.preload/3 with a query, or Ecto.Repo.preload/3 with a struct, but not the mixture.

Below is the variant with a query.

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
  • Related