rmp135 / sql-ts

Generate TypeScript interfaces from a SQL database.
MIT License
477 stars 65 forks source link

Duplicate fields are created in generated interface for table #112

Closed TimMikeladze closed 2 years ago

TimMikeladze commented 2 years ago

When running the CLI tool to generate types, the generated interface has multiple duplicate fields generated. I've included examples of my config and output below. My Postgres version is PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit and I'm using version 1.15.0 of the @rmp135/sql-ts package.

cli command

 sql-ts -c ./sql-ts.json

sql-ts.json

{
  "client": "pg",
  "connection": {
    "host": "localhost",
    "user": "postgres",
    "password": "postgres",
    "database" : "postgres"
  },
  "excludedTables": [
    "public.knex_migrations",
    "public.knex_migrations_lock"
  ]
}

table schema

postgres=# \d team_members
                           Table "public.team_members"
  Column   |           Type           | Collation | Nullable |      Default
-----------+--------------------------+-----------+----------+--------------------
 id        | uuid                     |           | not null | uuid_generate_v4()
 createdAt | timestamp with time zone |           | not null | CURRENT_TIMESTAMP
 updatedAt | timestamp with time zone |           | not null | CURRENT_TIMESTAMP
 role      | character varying(255)   |           | not null |
 userId    | uuid                     |           |          |
 teamId    | uuid                     |           |          |
Indexes:
    "team_members_pkey" PRIMARY KEY, btree (id)
    "team_members_userid_teamid_unique" UNIQUE CONSTRAINT, btree ("userId", "teamId")
Foreign-key constraints:
    "team_members_teamid_foreign" FOREIGN KEY ("teamId") REFERENCES teams(id) ON UPDATE CASCADE ON DELETE CASCADE
    "team_members_userid_foreign" FOREIGN KEY ("userId") REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE

generated types

export interface team_membersEntity {
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'createdAt'?: Date;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'id'?: string;
  'role': string;
  'teamId'?: string | null;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'updatedAt'?: Date;
  'userId'?: string | null;
}
rmp135 commented 2 years ago

I suspect this is caused by the changes made in 1.15 that attempt to extract default values.

Try with 1.14 and see if it still happens while I investigate.

TimMikeladze commented 2 years ago

1.14 works as expected.

rmp135 commented 2 years ago

Should be fixed in release 1.15.1.

TimMikeladze commented 2 years ago

Should be fixed in release 1.15.1.

Thank you!