sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.58k stars 812 forks source link

Return type of Postgres functions converting into interface{} instead of Struct. #3638

Closed anazcodes closed 1 month ago

anazcodes commented 1 month ago

Version

1.27.0

What happened?

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

CREATE TABLE IF NOT EXISTS public.users
(
    id bigserial NOT NULL,
    phone text ,
    created_at timestamp with time zone,
    updated_at timestamp with time zone,
    CONSTRAINT users_pkey PRIMARY KEY (id)
)

CREATE OR REPLACE FUNCTION public.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;
BEGIN
    INSERT 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$;

SQL queries

-- name: CreateUser :one
SELECT * FROM fn_authrepo_create_user($1, $2, $3);

Configuration

version: "2"
cloud:
  project: "<PROJECT_ID>"
sql:
- schema: "migrations/authdb/schema/*.sql"
  queries: "migrations/authdb/queries/*.sql"
  engine: "postgresql"
  gen:
    go: 
      package: "authdb"
      out: "internal/repository/authrepo/authdb"
      sql_package: "pgx/v5"
      emit_sql_as_comment: true
      emit_interface: true
      emit_prepared_queries: true

Playground URL

https://play.sqlc.dev/p/149e4929dbc625971466f68f4c1178337f2d4fb6e6168104aa2a007ff8f2f807

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

anazcodes commented 1 month ago

Figured out the way to do it