You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Return type of Postgres functions converting into interface{} instead of Struct. #3638
Sqlc not binding Postgres function's table type returning to a struct, it is directly binding into an interface{} type as an array.
Relevant log output
[5 2024-10-07 17:42:57.801246 +0530 IST 2024-10-07 17:42:57.801246 +0530 IST]
Database schema
CREATETABLEIF NOT EXISTS public.users
(
id bigserialNOT NULL,
phone text ,
created_at timestamp with time zone,
updated_at timestamp with time zone,
CONSTRAINT users_pkey PRIMARY KEY (id)
)
CREATE OR REPLACEFUNCTIONpublic.fn_authrepo_create_user(
var_phone text,
var_created_at timestamp with time zone,
var_updated_at timestamp with time zone)
RETURNS TABLE(user_id BIGINT, created_at timestamp with time zone, updated_at timestamp with time zone)
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
v_user_id BIGINT;
v_created_at TIMESTAMPTZ;
v_updated_at TIMESTAMPTZ;
BEGININSERT INTO users (phone, created_at, updated_at)
VALUES (var_phone, var_created_at, var_updated_at)
RETURNING users.id, users.created_at, users.updated_at
INTO v_user_id, v_created_at, v_updated_at;
RETURN QUERY SELECT v_user_id, v_created_at, v_updated_at;
END;
$BODY$;
@anazcodes Can you please explain how you made it? For now I have to write custom functions that return table into a separated .go file instead of using auto-generated one.
@anazcodes Can you please explain how you made it? For now I have to write custom functions that return table into a separated .go file instead of using auto-generated one.
@anazcodes this solution worked great for me, until I needed the function to return a nullable parameter.
The return model generated by sqlc always uses int32 instead of the nullable PgInt or a *int32.
And as soon as the procedure returns a null value, the app panics by trying to scan null into int32.
Any ideas how to adjust the query so that sqlc understands the return value is nullable?
Activity
anazrasheed commentedon Oct 7, 2024
Figured out the way to do it
RunicBean commentedon Dec 7, 2024
@anazcodes Can you please explain how you made it? For now I have to write custom functions that return table into a separated .go file instead of using auto-generated one.
anazrasheed commentedon Jan 2, 2025
@RunicBean
queries.sql
Define the returning fields like this with appropriate type casting.
schema.sql
Define the procedure in schema file.
Note: This does not work for procedures. Only works for PostgreSQL functions.
Hope this helps.
irreal commentedon May 19, 2025
@anazcodes this solution worked great for me, until I needed the function to return a nullable parameter.
The return model generated by sqlc always uses int32 instead of the nullable PgInt or a *int32.
And as soon as the procedure returns a null value, the app panics by trying to scan null into int32.
Any ideas how to adjust the query so that sqlc understands the return value is nullable?
anazrasheed commentedon May 20, 2025
Hi @irreal, you can use this configuration for reference.
Configuration
Try out with this, you may get corresponding
pgtypefor DB types, mention overrides with it.Here is what I achieved through this way (Auto Gen).
irreal commentedon May 20, 2025
Hi @anazcodes thank you so much!
emit_pointers_for_null_types: truedid not help, I think sqlc is just failing to understand it's nullable.however, I was able to override the type in the config:
this worked great.
I then did select id::int4 in the query.
for some reason, doing select id::integer and overriding "integer" in sqlc.yaml did not work.
but int4 is good.
It of course does the override even in other places, where the column is guaranteed not null, but that is acceptable to me.
thanks!