Filter date in query and format date

I’m doing a query and I need to format the time in the entity, but I’m getting this error:

DBConnection.EncodeError) Postgrex expected %Date{}, got “2021-10-25 01:55:18Z”. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.

def postegres(query, :from, dt),
    do: where(query, [op], fragment("?::dt", op.date) >= ^dt)

  def postegres(query, :from, dt) when is_binary(dt) do
      {:ok, datetime, _} = DateTime.from_iso8601(dt)

      postegres(query, :from, datetime)
    end

Your first function clause matches dt with anything. You would need to modify it to:

def postegres(query, :from, %DateTime{} = dt),
    do: where(query, [op], fragment("?::dt", op.date) >= ^dt)

Secondly, I’m not familiar with the Postgres type ::dt. I would expect it to be ::time or ::date or ::timestamp?:

kip=# \dT dt
     List of data types
 Schema | Name | Description 
--------+------+-------------
(0 rows)

Lastly, if op.date is a Postgres date type, I’m not sure it will allow you to compare a date with a timestamp. I haven’t tried this recently but its possible this will also return an error.

type ::date

Pattern-matching goes from most specific to least specific and follows the order with which the code use written.

Pattern-matching different function heads is happening top to bottom, so you need your more specific matching clauses to be on top. So swap the positions of the two variants of the function.