The schema:
export const myTable = mysqlTable(
"MyTable",
{
id: varchar("id", { length: 191 }).notNull(),
value: decimal("value", { precision: 7, scale: 4 }).notNull(),
createdAt: datetime("createdAt", { mode: "date" })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
(table) => {
return {
myTableId: primaryKey(table.id),
};
}
);
The code:
type MyTable = InferModel<typeof myTable, "select">;
const values: MyTable[] = await db.select().from(myTable);
The type of values[0].value
is string
, and I figure it should be a number
.
I could not find anything related to this on Drizzle docs, Github Issues or StackOverflow, and I would like to understand why this happens, or, if I'm making any mistakes.
EDIT: I added an answer that "fixes" the type, but does not answer why double
becomes a number
and decimal
becomes a string
, which is enough for me.
EDIT 2: Thanks @ColouredPanda and @andrew-allen: https://github.com/drizzle-team/drizzle-orm/issues/570#issuecomment-1646033240
This might not be an answer to the "why decimals become strings", but it answers the typing problem, which was my main concern.
Instead of decimal
, use double
. Drizzle will automatically infer double
to number
.
The updated schema:
export const myTable = mysqlTable(
"MyTable",
{
id: varchar("id", { length: 191 }).notNull(),
value: double("value", { precision: 7, scale: 4 }).notNull(),
createdAt: datetime("createdAt", { mode: "date" })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
(table) => {
return {
myTableId: primaryKey(table.id),
};
}
);