Vovchikan

Vovchikan

Interpolate Ecto.Enum in fragment/1

I have tried this code:

def my_func(params) do
  ...
  |> select(
        [l],
        %{
          date: fragment("date_trunc('?',?) as date",
            ^params.date_trunc_type,
            l.inserted_at),
        }
      )
  ...
  |> Repo.all()
end

But i’m stacked at this error:

** (Postgrex.Error) ERROR 42P18 (indeterminate_datatype) could not determine data type of parameter $1

query: SELECT date_trunc('$1',s0."inserted_at") as date...

My Modules:

defmodule Administration.Reports.Params do
  ...

  alias Administration.Enum.DateTruncType
  embedded_schema do
    ...
    field :date_trunc_type, DateTruncType, default: :month
  end
  
  ...
end

defmodule Administration.Enum.DateTruncType do
  use EctoEnum,
    hour: "hour",
    day: "day",
    month: "month",
    year: "year"
end

I want to construct this sql’s SELECTs

SELECT date_trunc('hour', inserted_at)
SELECT date_trunc('day', inserted_at)
SELECT date_trunc('month', inserted_at)
SELECT date_trunc('year', inserted_at)

Most Liked

joey_the_snake

joey_the_snake

When you are using fragments Ecto doesn’t know what type you are using so it won’t convert the atom to string. You need to tell it the type using type/2. This can be either a field name or a type name.

More info: Ecto.Query.API — Ecto v3.14.0

LostKobrakai

LostKobrakai

Also remove the quotes around the parameter. If the parameter is a string postgres can deal with that without you putting quotes up.

Last Post!

Vovchikan

Vovchikan

%{
        date: fragment("date_trunc(?,?) as date",
          type(^params.date_trunc_type, DateTruncType),
          l.inserted_at)
}

This worked! I tried type/2 before like this, but this didn’t work. Mb @LostKobrakai advice helped or i’ve used type/2 wrong.

@LostKobrakai, @joey_the_snake ty for help.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement