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.10.3

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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement