drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.56k stars 579 forks source link

[BUG]: Invalid schema generation on postgres instrospect #2337

Closed duckboy81 closed 4 months ago

duckboy81 commented 4 months ago

What version of drizzle-orm are you using?

v0.20.18

What version of drizzle-kit are you using?

v0.30.10

Describe the Bug

Smallest of issues: During introspection, the resultant schema.ts is missing a comma in the extraConfig section for a PgTable.

Given the following database table:

create table public.organization_code_configuration
(
    code_config_id         integer                  not null
        constraint organization_code_configuration_code_configuration_code_config_
            references public.code_configuration
            on update cascade on delete restrict,
    org_id                 uuid                     not null
        constraint organization_code_configuration_organization_org_id_fk
            references public.organization
            on update cascade on delete cascade,
    effective              date                     not null,
    last_updated           timestamp with time zone not null,
    last_updated_by        uuid,
    can_edit_configuration boolean default false    not null,
    constraint organization_code_configuration_pk
        primary key (org_id, code_config_id),
    constraint organization_code_configuration_pk_2
        unique (org_id, effective)
);

comment on constraint organization_code_configuration_code_configuration_code_config_ on public.organization_code_configuration is 'prevents deletion of a code configuration while an organization still has a link to it';

alter table public.organization_code_configuration
    owner to postgres;

Run drizzle-kit introspect:pg

Expected behavior

Current behavior. Notice the missing comma after name: "organization_code_configuration_pk"}) (4th line from the bottom)

export const organizationCodeConfiguration = pgTable("organization_code_configuration", {
    codeConfigId: integer("code_config_id").notNull().references(() => codeConfiguration.codeConfigId, { onDelete: "restrict", onUpdate: "cascade" } ),
    orgId: uuid("org_id").notNull().references(() => organization.orgId, { onDelete: "cascade", onUpdate: "cascade" } ),
    effective: date("effective").notNull(),
    lastUpdated: timestamp("last_updated", { withTimezone: true, mode: 'string' }).notNull(),
    lastUpdatedBy: uuid("last_updated_by"),
    canEditConfiguration: boolean("can_edit_configuration").default(false).notNull(),
},
(table) => {
    return {
        organizationCodeConfigurationPk: primaryKey({ columns: [table.codeConfigId, table.orgId], name: "organization_code_configuration_pk"})
        organizationCodeConfigurationSingleConfigPerDay: unique("organization_code_configuration_single_config_per_day").on(table.orgId, table.effective),
    }
});

Environment & setup

Windows 11, node v20.13.1, cli

AlexBlokh commented 4 months ago

can you please check on version drizzle-kit@0.21.3, should be fixed, we didn't manage to reproduce with your schema