danie...@gmail.com
unread,Dec 10, 2025, 5:33:18 PM (6 days ago) Dec 10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to elixir-ecto
By defaut ecto postgrex adapter casts integer to bigint and this is not always what we want to do. I know that changing this is not possible at this point but maybe we could we add the aliases like int2, int4 and int8 that are supported by postgres and are explicit about the data type.
To cast to integer I need to create a custom type and use an atom that is not in the list - :int in this case that falls into Atom.to_string/1 logic
Schema |> select([t], type(t.x, :integer)) |> plan() |> all()
SELECT s0."x"::bigint FROM "schema" AS s0
defmodule Int do def type, do: :int end
Schema |> select([t], type(t.x, Int)) |> plan() |> all()
SELECT s0."x"::int FROM "schema" AS s0
Schema |> select([t], type(t.x, :int)) |> plan() |> all()
this fails with:
** (UndefinedFunctionError) function :int.type/0 is undefined (module :int is not available)
The problem is that by default postgres does not support all casts - for example boolean can be casted only to integer or text but not to bigint.
postgres@localhost:postgres> SELECT
pg_catalog.format_type(castsource, NULL) AS source_type,
pg_catalog.format_type(casttarget, NULL) AS target_type
FROM pg_cast WHERE pg_catalog.format_type(castsource, NULL) = 'boolean'
source_type target_type
----------- -----------------
boolean integer
boolean text
boolean character varying
boolean character
SELECT 4
postgres@localhost:postgres> select true::integer;
+------+
| int4 |
|------|
| 1 |
+------+
SELECT 1
Time: 0.005s
postgres@localhost:postgres> select true::int4;
+------+
| int4 |
|------|
| 1 |
+------+
SELECT 1
Time: 0.008s
postgres@localhost:postgres> select true::text;
+------+
| text |
|------|
| true |
+------+
SELECT 1
Time: 0.006s
postgres@localhost:postgres> select true::bigint;
cannot cast type boolean to bigint
LINE 1: select true::bigint
^
Time: 0.003s