DT
Drizzle Team•2mo ago
Simon

Drizzle with Zod throws type errors with simple `numeric` example

For example, I define a PostgreSQL table with an input schema:
export const Foo = pgTable('foo', {
bar: numeric('bar'),
});
export const CreateFooSchema = createInsertSchema(Foo);
export const Foo = pgTable('foo', {
bar: numeric('bar'),
});
export const CreateFooSchema = createInsertSchema(Foo);
When using this in my API:
create: protectedProcedure
.input(CreateFooSchema)
.mutation(({ ctx, input }) => {
return ctx.db.insert(Foo).values(input);
}),
create: protectedProcedure
.input(CreateFooSchema)
.mutation(({ ctx, input }) => {
return ctx.db.insert(Foo).values(input);
}),
I get a type error thrown:
Types of property 'bar' are incompatible.
Type 'string | null | undefined' is not assignable to type 'string | SQL<unknown> | Placeholder<string, any> | null'.
Types of property 'bar' are incompatible.
Type 'string | null | undefined' is not assignable to type 'string | SQL<unknown> | Placeholder<string, any> | null'.
This is specifically a problem with the numeric and decimal data types. Other standard data types work fine. Any ideas on a fix?
7 Replies
Simon
Simon•2mo ago
Bump
pacto
pacto•2mo ago
your numeric data can be null when you are grabbing it from db, you can either make it notNull in drizzle schema or nullable in zod to allow null values
Simon
Simon•2mo ago
@pacto shouldn't createInsertSchema infer the schema from the pgTable definition?
johtso
johtso•2mo ago
@Simon did you find a solution? I'm having the same problem with a custom Date datatype
const date = customType<
{ data: Date; driverData: string; }
>({
dataType() {
return 'text';
},
fromDriver(value: string): Date {
return new Date(value);
},
toDriver(value: Date): string {
if (value.getHours() !== 0 || value.getMinutes() !== 0 || value.getSeconds() !== 0 || value.getMilliseconds() !== 0) {
throw new Error('Date must have a time of 00:00:00.000');
}
try {
return value.toISOString().split('T')[0] as string;
} catch (e) {
console.log(`failed to covert ${JSON.stringify(value)} to iso string`)
throw e;
}
},
});
const date = customType<
{ data: Date; driverData: string; }
>({
dataType() {
return 'text';
},
fromDriver(value: string): Date {
return new Date(value);
},
toDriver(value: Date): string {
if (value.getHours() !== 0 || value.getMinutes() !== 0 || value.getSeconds() !== 0 || value.getMilliseconds() !== 0) {
throw new Error('Date must have a time of 00:00:00.000');
}
try {
return value.toISOString().split('T')[0] as string;
} catch (e) {
console.log(`failed to covert ${JSON.stringify(value)} to iso string`)
throw e;
}
},
});
export const emails = sqliteTable('emails', {
idempotencyKey: text('idempotency_key').primaryKey(),
membershipEndDate: date('membership_end_date')
});
export const emails = sqliteTable('emails', {
idempotencyKey: text('idempotency_key').primaryKey(),
membershipEndDate: date('membership_end_date')
});
export const insertEmailSchema = createInsertSchema(emails, {
timeSent: z.date(),
membershipEndDate: z.date(),
}).strict();
export type InsertEmail = z.infer<typeof insertEmailSchema>;
export const insertEmailSchema = createInsertSchema(emails, {
timeSent: z.date(),
membershipEndDate: z.date(),
}).strict();
export type InsertEmail = z.infer<typeof insertEmailSchema>;
export const createEmail = async (
db: dbType,
email: InsertEmail
): Promise<boolean> => {
const result = await db
.insert(emails)
.values(email)
return result.results.length > 0;
}
export const createEmail = async (
db: dbType,
email: InsertEmail
): Promise<boolean> => {
const result = await db
.insert(emails)
.values(email)
return result.results.length > 0;
}
Types of property 'membershipEndDate' are incompatible.
Type 'Date | null | undefined' is not assignable to type 'Date | SQL<unknown> | Placeholder<string, any> | null'.
Type 'undefined' is not assignable to type 'Date | SQL<unknown> | Placeholder<string, any> | null'.
Types of property 'membershipEndDate' are incompatible.
Type 'Date | null | undefined' is not assignable to type 'Date | SQL<unknown> | Placeholder<string, any> | null'.
Type 'undefined' is not assignable to type 'Date | SQL<unknown> | Placeholder<string, any> | null'.
ahh.. I think it's exactOptionalPropertyTypes in tsconfig
Simon
Simon•2mo ago
Yeah that is what was throwing the TypeError 🙃
Amos
Amos•2d ago
Did you manage to fix it? I guess you are also using @effect/schema as I've seen you there too 😛 but they require that exactOptionalPropertyTypes I think?
johtso
johtso•2d ago
I'm not really using effect schema with drizzle. I think I'm just not using the zod schema at all, instead just explicitly passing the relevant key/values as the values argument in my query..
Want results from more Discord servers?
Add your server
More Posts
Why is it the case that my IDE says `await` does nothing on pretty much every single function?I'm trying to setup a startup script that creates a local db and seeds it when I start my project. IHow to do and "inArray" with a dinamic array?I did this on the code and is working well, the things is that it throws and error and idk how to fitoo many clients already, remaining slots reserved for superuserI have a production deployment where I'm facing this issue. I ran `SELECT count(*) FROM pg_stat_actiWeird freeze in code, seemingly stalling on drizzle queryMy function runs a few queries to the database, but they don't seem to basically ever finish. I don'Typescript ErrorAnyone know why this is happening? I upgraded my drizzle version so maybe something changed?Warning: You need to pass an instance of Client:ClientCould someone explain / show me what changes I need to apply to the config file? Thanks in advance! Prepared insert statement with customTypeWhen using prepared insert statement with customType the `Placeholder` object gets passed to the `toDrizzle ORM + Supabase Enforce SSLI'm trying to connect with a direct pool connection (transaction mode). I'm deploying to Supabase DeCan't infer relationship one-to-oneI'm trying to follow the relationship docs to set a relationship between a user and a role, but I'm Cannot set null value to FK - null value in column "X" of relation "Y" violates not-null constraintHi everyone, I'm trying to create a userId column, fk to users.id but optional (so I don't set the n