Skip to content

[BUG]: Columns defined with text() get converted to a schema of type string | null | undefined #3

@alveshelio

Description

@alveshelio

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.44.2

What version of drizzle-kit are you using?

0.31.2

Other packages

No response

Describe the Bug

I have this table:

export const unitsI18N = pgTable(
  'units_i18n',
  {
    id: text()
      .primaryKey()
      .$defaultFn(() => DbUtils.cuid2()),
    dataId: text()
      .notNull()
      .references(() => units.id, {
        onDelete: 'cascade',
        onUpdate: 'cascade',
      }),
    locale: text()
      .notNull()
      .references(() => locales.code, { onUpdate: 'cascade' }),
    name: text(),
    description: text(),
  },
  (table) => [
    {
      uniqueDataIdLocale: unique().on(table.dataId, table.locale),
    },
  ],
);

When I do export const DbUnitInputSchema = createInsertSchema(units);
And then I do type UnitTranslation = Schema.Schema.Type<typeof DbUnitInputSchema>
Then I get a schema definition like so:

Schema.Struct({
  locale: Schema.String,
  dataId: Schema.String,
  name: Schema.NullishOr(Schema.String),
  description: Schema.NullishOr(Schema.String),
  otherNames: Schema.NullishOr(Schema.String),
  acronyms: Schema.NullishOr(Schema.String),
});

which gives this type:

{
  locale: string;
  dataId: string;
  name: string | null | undefined;
  description: string | null | undefined;
}

Ideally it should be:

Schema.Struct({
  locale: Schema.String,
  dataId: Schema.String,
  name: Schema.NullOr(Schema.String),
  description: Schema.NullOr(Schema.String),
  otherNames: Schema.NullOr(Schema.String),
  acronyms: Schema.NullOr(Schema.String),
});

Which would give this type signature.

{
  locale: string;
  dataId: string;
  name: string | null;
  description: string | null;
}

Is this a bug or the default behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions