Drizzle Schema and TS

Hello, I have a question around how are we expected to consume the types from the schema generated by drizzle-kit pull .
// schema.ts
export const user_points = pgTable(
'user_points',
{
user_id: uuid().primaryKey().notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
current: bigint({ mode: 'number' }).default(0).notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
spent: bigint({ mode: 'number' }).default(0).notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
total: bigint({ mode: 'number' }).default(0).notNull(),
updated_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow(),
created_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow(),
},
(table) => [
foreignKey({
columns: [table.user_id],
foreignColumns: [users.id],
name: 'user_points_user_id_fkey',
}).onDelete('cascade'),
check('user_points_current_check', sql`current >= 0`),
check('user_points_spent_check', sql`spent >= 0`),
check('user_points_total_check', sql`total >= 0`),
],
);
// schema.ts
export const user_points = pgTable(
'user_points',
{
user_id: uuid().primaryKey().notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
current: bigint({ mode: 'number' }).default(0).notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
spent: bigint({ mode: 'number' }).default(0).notNull(),
// You can use { mode: "bigint" } if numbers are exceeding js number limitations
total: bigint({ mode: 'number' }).default(0).notNull(),
updated_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow(),
created_at: timestamp({ withTimezone: true, mode: 'string' }).defaultNow(),
},
(table) => [
foreignKey({
columns: [table.user_id],
foreignColumns: [users.id],
name: 'user_points_user_id_fkey',
}).onDelete('cascade'),
check('user_points_current_check', sql`current >= 0`),
check('user_points_spent_check', sql`spent >= 0`),
check('user_points_total_check', sql`total >= 0`),
],
);
then on the service if we want run an update operation:
import { user_points } from '@/src/drizzle/schema';

// ✅ this will work
const [updated] = await db
.update(user_points)
.set({
[user_points.current.name]: newCurrent,
[user_points.spent.name]: newSpent,
[user_points.updated_at.name]: new Date(),
})
.where(eq(user_points.user_id, userId))
.returning();


// 💢 but used as:
const [updated] = await db
.update(user_points)
.set({
current: newCurrent,
spent: newSpent,
updated_at: new Date(),
})
.where(eq(user_points.user_id, userId))
.returning();

// will fail to infer the other columns and only `user_id` relation will be inferred
Object literal may only specify known properties, and 'current' does not exist in type '{ user_id?: string | SQL<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>; }'.ts(2353)
import { user_points } from '@/src/drizzle/schema';

// ✅ this will work
const [updated] = await db
.update(user_points)
.set({
[user_points.current.name]: newCurrent,
[user_points.spent.name]: newSpent,
[user_points.updated_at.name]: new Date(),
})
.where(eq(user_points.user_id, userId))
.returning();


// 💢 but used as:
const [updated] = await db
.update(user_points)
.set({
current: newCurrent,
spent: newSpent,
updated_at: new Date(),
})
.where(eq(user_points.user_id, userId))
.returning();

// will fail to infer the other columns and only `user_id` relation will be inferred
Object literal may only specify known properties, and 'current' does not exist in type '{ user_id?: string | SQL<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>; }'.ts(2353)
Why is that? How we should correctly use it? Thanks in advance 🙂
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?