Creating One-to-Many relations

Question regarding creating relations. I have created a usersTable that has an id, I am currently trying to create a suggestionsTable and add relations to the usersTable in the suggestionsTable: I have the following but I am getting an error on the fields property that says that it doesn’t exist. import { pgTable, text, integer, timestamp } from "drizzle-orm/pg-core"; import { relations } from "drizzle-orm"; import { usersTable } from "./user"; export const suggestionsTable = pgTable("suggestions", { id: integer("id").primaryKey().notNull(), userId: integer("user_id").notNull(), content: text("content").notNull(), timestamp: timestamp("timestamp").notNull(), }); export const usersRelations = relations(usersTable, ({ one, many }) => ({ suggestions: many(suggestionsTable, { fields: [suggestionsTable.userId], references: [usersTable.id], }), })); Picture for reference:
No description
2 Replies
gneiru
gneiru13mo ago
When using many, you won't need fields and references. Just many(suggestionsTable) is enough, then might also wanna add relations for suggestions table export const suggestionsRelations = relations(suggestionsTable, ({ one }) => ({ user: one(usersTable, { fields: [suggestionsTable.userId], references: [usersTable.id], }), })); Docs
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
PatrickVM
PatrickVMOP13mo ago
Thank you gneiru for your time. Will implement this tonight.

Did you find this page helpful?