pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Column of type character(3) gets converted to `never` type

Open golergka opened this issue 5 years ago • 3 comments

Steps to reproduce

In my db schema, I have the following table:

CREATE TABLE public.countries
(
    id integer NOT NULL,
    iso3 character(3) COLLATE pg_catalog."default",
    CONSTRAINT countries_pkey PRIMARY KEY (id)
)

In my code, I write the following query:

const getCountryIso = sql`SELECT iso3 FROM countries WHERE id = $id`

Expected result

I would expect the generated code to use string:

/** 'GetCountryIso' return type */
export interface IGetCountryIsoResult {
  iso3: string | null;
}

Actual result

However, the generated code returns iso3 column as never type:

/** 'GetCountryIso' return type */
export interface IGetCountryIsoResult {
  iso3: never | null;
}

golergka avatar Aug 05 '20 13:08 golergka

I have prepared a failing unit test for this issue: https://github.com/golergka/pgtyped/tree/153-fixed-character-type

I would love to implement it myself, but it seems it would require a re-design of DefaultTypeMapping and TypeAllocator. At the moment, they're working with string constants as type signatures, and supporting character(n) type signature would require using regexp instead. I don't want to implement such drastic changes without maintainer's design approval.

golergka avatar Aug 05 '20 14:08 golergka

Thanks for preparing a test case @golergka Unfortunately I can't reproduce the issue in the example docker container (Postgres 12). When I try it, pgTyped correctly infers the type as string. Which Postgres version are you using? Can you reproduce the issue by adding a character(3) column on the example's container schema.sql?

Looking through the logs, I see that for a character(3) column Postgres returns the internal column type as pbchar, which is currently mapped to string.

adelsz avatar Aug 05 '20 20:08 adelsz

Which Postgres version are you using?

I'm testing it on PostgreSQL 12.2.

Can you reproduce the issue by adding a character(3) column on the example's container schema.sql?

Added it to my pull request.

Looking through the logs, I see that for a character(3) column Postgres returns the internal column type as pbchar, which is currently mapped to string.

I feel that my knowledge of Postgres is not enough to talk about this - I'll check the documentation to figure out what "internal column type" is and how to test it and get back to you.

golergka avatar Aug 06 '20 19:08 golergka

Closing as stale.

adelsz avatar Jan 29 '23 01:01 adelsz