Can't figure out this simple one-to-many relationship
Hey, so i have two tables, a patient one and a village one.
I just want to define a simple one-to-many, one village for many patients, what am I missing here? My villageRelations is throwing type errors
export const patient = mysqlTable("patient", {
id: serial("id").primaryKey(),
firstName: varchar("firstName", { length: 256 }).notNull(),
middleName: varchar("middleName", { length: 256 }),
lastName: varchar("lastName", { length: 256 }).notNull(),
dob: date("dob", { mode: "date" }).notNull(),
sex: varchar("sex", { length: 32 }),
villageId: int("villageId"),
createdAt: datetime("createdAt", { mode: "date", fsp: 3 })
.default(sql
CURRENT_TIMESTAMP(3))
.notNull(),
updatedAt: datetime("updatedAt", { mode: "date", fsp: 3 })
.default(sql
CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3))
.notNull(),
});
export const patientRelations = relations(patient, ({ one }) => ({
village: one(village, {
fields: [patient.villageId], // Referencing villageId in patient table
references: [village.id], // Referencing id in village table
}),
}));
and
export const village = mysqlTable("village", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }).notNull(),
region: varchar("region", { length: 256 }),
population: int("population"),
createdAt: datetime("createdAt", { mode: "date", fsp: 3 })
.default(sql
CURRENT_TIMESTAMP(3))
.notNull(),
updatedAt: datetime("updatedAt", { mode: "date", fsp: 3 })
.default(sql
CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3))
.notNull(),
});
export const villageRelations = relations(village, ({ many }) => ({
patients: many(patient, {
fields: [patient.villageId], // Referencing villageId in patient table
references: [village.id], // Referencing id in village table
}),
}));
1 Reply
I did this instead:
export const villageRelations = relations(village, ({ many }) => ({
patients: many(patient),
}));
please advice if this is correct?