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:2 Replies
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],
}),
}));
DocsDrizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thank you gneiru for your time. Will implement this tonight.