sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Incorrect datatype assigned to a column of type smallint[]

Open GetPsyched opened this issue 3 years ago • 4 comments

Version

1.13.0

What happened?

I have a column (credits) in a PostgreSQL table whose datatype is smallint[4], an array of 4 smallint types.

When I put the query (given below), a struct is generated and that column has the datatype []int16. The problem I was facing is that the returned object of that struct contained only the values until that column (excluding it). I spent some time figuring out the issue and at the end I found that changing the datatype to []int32 in the auto-generated struct fixed the issue and I got my data as normal.

However, this isn't ideal as the auto-generated struct should not be edited.

Relevant log output

No response

Database schema

CREATE TABLE IF NOT EXISTS course (
    code        char(7) NOT NULL,
    title       varchar(32) NOT NULL,
    branch      char(3) NOT NULL,
    semester    smallint NOT NULL,
    credits     smallint[4] NOT NULL,
    prereq      char(7)[],
    type        char(3) NOT NULL,
    objectives  text NOT NULL,
    content     text NOT NULL,
    books       text NOT NULL,
    outcomes    text NOT NULL,
    PRIMARY KEY (code, branch)
);

SQL queries

-- name: GetAllCourses :many
SELECT * FROM course;

Configuration

version: 1
packages:
  - path: "query"
    name: "query"
    engine: "postgresql"
    schema: "query/schema.sql"
    queries: "query/query.sql"

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

GetPsyched avatar May 16 '22 15:05 GetPsyched

smallint maps to a int16, so I'm not sure why changing the array to int32 was necessary. Will take a look

kyleconroy avatar Jun 04 '22 15:06 kyleconroy

Any updates yet?

GetPsyched avatar Jun 22 '22 19:06 GetPsyched

I did some research at it looks like this happens due to the lib/pg driver that is used in your example. For some reason this driver only supports int32 and int64 in arrays (see array.go). When I add an Int16Array there, the struct above can be Scanned. There is an open (ticket) where someone asks about this.

When you use sql_package: "pgx/v4" to generate the code, the example works.

To me it seems that the proper way to fix this is the lib/pg driver. Generating int32 for smallint is just a workaround.

akutschera avatar Jul 05 '22 13:07 akutschera

Gotcha. I just hope that the lib/pg devs fix it then, @akutschera?

GetPsyched avatar Jul 07 '22 12:07 GetPsyched