go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

character varying = typeid error: Recipe `jet` failed on line 52 with exit code 1 #284

Closed spa5k closed 8 months ago

spa5k commented 8 months ago

Describe the bug It's giving error while generating the types

Connecting to postgres database...
Retrieving schema information...
Error trace:
 - failed to get 'public' schema metadata:
 - failed to get public tables metadata:
 - failed to query BASE TABLE metadata:
 - jet:
 - pq:
 - operator is only a shell:
 - character varying = typeid
error: Recipe `jet` failed on line 52 with exit code 1

Environment (please complete the following information):

Code snippet


CREATE TABLE public.application_user (
    id public.application_user_id DEFAULT public.typeid_generate('application_user'::text) NOT NULL,
    customer_id public.customer_id NOT NULL,
    access_failed_count integer DEFAULT 0 NOT NULL,
    email text NOT NULL,
    email_confirmed boolean DEFAULT false NOT NULL,
    lockout_enabled boolean DEFAULT false NOT NULL,
    lockout_end timestamp with time zone,
    normalized_email text NOT NULL,
    normalized_user_name text NOT NULL,
    password_hash text NOT NULL,
    phone_number text,
    phone_number_confirmed boolean DEFAULT false NOT NULL,
    user_name text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

https://github.com/jetpack-io/typeid

Using this as the primary key Expected behavior It should translate the typeid to text

go-jet commented 8 months ago

Hi @spa5k, I'm having a problem to reproduce the bug. Generator successfully generates code for application_user table. Could you share how application_user_id and customer_id is defined. Error trace points to operator error. It seems you are trying to compare somewhere typeid and varchar, which is not allowed.

spa5k commented 8 months ago

Hey, the tables are like this -


CREATE DOMAIN user_id AS typeid CHECK (typeid_check(value, 'user'));

create table "user"
(
    id                     user_id primary key not null default typeid_generate('user'),
    access_failed_count    integer             not null default 0,
    created_at             timestamptz         not null default now(),
    updated_at             timestamptz         not null default now()
);

I'm using the typeid from the jetpack-io/typeid as Primary key, so the type it returns is basically a tuple of string and uuid.

spa5k commented 8 months ago

Hey I was able to fix it up, so basically in this file - https://github.com/jetpack-io/typeid-sql/blob/main/sql/03_typeid.sql, they were using VARCHAR and sometimes with fixed length of 63, I basically replaced it with text and it worked.