Using default values removes columns from insert and update types

export const testSchema = pgTable('test_table', {
defaultValue: integer('default_value').notNull().default(1),
nonDefaultValue: integer('non_default_value').notNull(),
});

db.insert(testSchema).values({
defaultValue: 2, // this errors
nonDefaultValue: 1,
});
export const testSchema = pgTable('test_table', {
defaultValue: integer('default_value').notNull().default(1),
nonDefaultValue: integer('non_default_value').notNull(),
});

db.insert(testSchema).values({
defaultValue: 2, // this errors
nonDefaultValue: 1,
});
Using the above schema makes the following error when I try to override the defaultValue with 2.
No overload matches this call.
Overload 1 of 2, '(value: { nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }): PgInsertBase<PgTableWithColumns<{ name: "test_table"; schema: undefined; columns: { defaultValue: PgColumn<{ name: "default_value"; ... 12 more ...; generated: GeneratedColumnConfig<...>; }, {}, {}>; nonDefaultValue: PgColumn<...>; }; dialect: "pg"; }>, NodePgQueryResultHKT, undefined, false, never>', gave the following error.
Object literal may only specify known properties, but 'defaultValue' does not exist in type '{ nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }'. Did you mean to write 'nonDefaultValue'?
Overload 2 of 2, '(values: { nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }[]): PgInsertBase<PgTableWithColumns<{ name: "test_table"; schema: undefined; columns: { defaultValue: PgColumn<{ name: "default_value"; ... 12 more ...; generated: GeneratedColumnConfig<...>; }, {}, {}>; nonDefaultValue: PgColumn<...>; }; dialect: "pg"; }>, NodePgQueryResultHKT, undefined, false, never>', gave the following error.
Object literal may only specify known properties, and 'defaultValue' does not exist in type '{ nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }[]'.
No overload matches this call.
Overload 1 of 2, '(value: { nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }): PgInsertBase<PgTableWithColumns<{ name: "test_table"; schema: undefined; columns: { defaultValue: PgColumn<{ name: "default_value"; ... 12 more ...; generated: GeneratedColumnConfig<...>; }, {}, {}>; nonDefaultValue: PgColumn<...>; }; dialect: "pg"; }>, NodePgQueryResultHKT, undefined, false, never>', gave the following error.
Object literal may only specify known properties, but 'defaultValue' does not exist in type '{ nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }'. Did you mean to write 'nonDefaultValue'?
Overload 2 of 2, '(values: { nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }[]): PgInsertBase<PgTableWithColumns<{ name: "test_table"; schema: undefined; columns: { defaultValue: PgColumn<{ name: "default_value"; ... 12 more ...; generated: GeneratedColumnConfig<...>; }, {}, {}>; nonDefaultValue: PgColumn<...>; }; dialect: "pg"; }>, NodePgQueryResultHKT, undefined, false, never>', gave the following error.
Object literal may only specify known properties, and 'defaultValue' does not exist in type '{ nonDefaultValue: number | SQL<unknown> | Placeholder<string, any>; }[]'.
And if I remove the notNull on the nonDefaultValue I get no intelisense at all. I can input any object and typescript shows no errors.
db.insert(testSchema).values({
defaultValue: 2,
nonDefaultValue: 1,
blabla: 'blabla', // no ts errors
});
db.insert(testSchema).values({
defaultValue: 2,
nonDefaultValue: 1,
blabla: 'blabla', // no ts errors
});
I hope you can help me. Thanks in advance!
6 Replies
CrashChicken
CrashChickenOP4mo ago
Also I'm using this inside a NestJS project if that is a factor and the latest drizzle-orm 0.33.0
francis
francis4mo ago
interesting. what driver? and what is the type generated by typeof testSchema.$inferSelect and typeof testSchema.$inferInsert
CrashChicken
CrashChickenOP4mo ago
If I have notNull on both columns its:
type Test = {
defaultValue: number;
nonDefaultValue: number;
}
type Test = {
defaultValue: number;
nonDefaultValue: number;
}
type NewTest = {
nonDefaultValue: number;
}
type NewTest = {
nonDefaultValue: number;
}
But if I remove notNull on the nonDefaultValue column inferInsert is empty:
type NewTest = {}
type NewTest = {}
and i'm using pgTable with pg (node-postgres) Hmmm, I tested this exact code in a nextjs project and it works prfectly. It seems that nestjs has some wierd ts behaviors?
type NewTest = {
defaultValue?: number | undefined;
nonDefaultValue?: number | null | undefined;
}
type NewTest = {
defaultValue?: number | undefined;
nonDefaultValue?: number | null | undefined;
}
Created brand new nestjs project and it has the same problems, i guess i have to look into tsconfig.json omg, fixed it by enabling strictNullChecks as nestjs defaults it to false
francis
francis4mo ago
ah yep that'll do it in general, turn strict mode on
CrashChicken
CrashChickenOP4mo ago
yeah unfortunately nestjs and some of its packages have problems in this mode and thats why they dont turn this on by default will probably ditch it in favor of hono if it causes too much issues
huge
huge2mo ago
ay found any solution,?
Want results from more Discord servers?
Add your server