sadmann7 / shadcn-table

Shadcn table with server-side sorting, filtering, and pagination.

Home Page:https://table.sadmn.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feat]: Relations / references

t1nky opened this issue · comments

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

you can just do a left join. to join the tasks table with the user table.

you can show whatever that way