[feat]: Relations / references
t1nky opened this issue · 2 comments
t1nky commented
Feature description
I have a table with userId
field, but it is not helpful to display it as is, instead It would be nice to have the ability to easily add columns from the referenced table (user.name
):
export const users = pgTable("users", {
id: serial("id").primaryKey(),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").default(sql`current_timestamp`),
})
export const tasks = pgTable("tasks", {
id: varchar("id", { length: 30 })
.$defaultFn(() => generateId())
.primaryKey(),
// ...
userId: integer("user_id").notNull().references(() => users.id),
createdAt: timestamp("created_at").defaultNow().notNull(),
updatedAt: timestamp("updated_at").default(sql`current_timestamp`),
})
export const tasksRelations = relations(tasks, ({ one }) => ({
user: one(users, {
fields: [tasks.userId],
references: [users.id],
}),
}));
p.s. I've tried it myself, it did work, but it's very hacky and also type for DataTableFilterField.value
had to be changed to string, because the id of my column was user_name
as a result of referencing
Additional Context
Also, maybe we can utilize dynamic query building for better types and more reusability:
https://orm.drizzle.team/docs/dynamic-query-building
Before submitting
- I've made research efforts and searched the documentation
- I've searched for existing issues and PRs
sadmann7 commented
you can just do a left join. to join the tasks table with the user table.
you can show whatever that way
sadmann7 commented
you can use the accessor function in the column definition to transform the key