Doron Torangy
Doron Torangy
DTDrizzle Team
Created by Doron Torangy on 2/4/2024 in #help
Self referencing column breaks table type in typescript
I have the following table:
export const categoriesTable = pgTable(
'categories',
{
id: uuid('id').primaryKey(),
name: varchar('name', { length: 50 }).notNull(),
description: text('description'),
parentId: uuid('parent_id').references(() => categoriesTable.id, { onDelete: 'set null' }),
createdAt: timestamp('created_at').defaultNow()
},
(table) => ({
nameIdx: uniqueIndex('name_idx').on(table.name)
})
)
export const categoriesTable = pgTable(
'categories',
{
id: uuid('id').primaryKey(),
name: varchar('name', { length: 50 }).notNull(),
description: text('description'),
parentId: uuid('parent_id').references(() => categoriesTable.id, { onDelete: 'set null' }),
createdAt: timestamp('created_at').defaultNow()
},
(table) => ({
nameIdx: uniqueIndex('name_idx').on(table.name)
})
)
However typescript infers the type of categoriesTable as any It's only when I remove .references(() => categoriesTable.id, { onDelete: 'set null' }) that typescript infers the type correctly If so, how can I still create a self reference in this case?
2 replies
DTDrizzle Team
Created by Doron Torangy on 7/14/2023 in #help
Get type for select query?
Hey guys, is there a way to infer the "select" type for a given table? for example:
async getUsers(selectColumns: ?) {
return this.db.select(selectColumns).from(users)
}
async getUsers(selectColumns: ?) {
return this.db.select(selectColumns).from(users)
}
I have a UsersService class with getUsers method. I want callers to be able to select specific columns. Is is possible to achieve this type safety and autocomplete without directly using the drizzle client?
6 replies