Drizzle ORM: Inferring Relation multiple Many to one relation

Hi everyone, I'm working on a project using Drizzle ORM in a Next.js environment and I've run into a snag. My goal is to set up a database schema for an educational platform where courses have dependencies on other courses. Specifically, I'm trying to establish two types of relationships in my courses table: Course Dependencies: Identifying the prerequisite courses for a given course. Dependent Courses: Listing the courses that depend on a given course as a prerequisite. However, I'm encountering an error when trying to infer the relation for dependentCourses in my courses table. Here's the error message I received: Error
Error: There is not enough information to infer relation "__public__.courses.dependentCourses"
at normalizeRelation [...]
at Command.<anonymous> [...]

Node.js v21.4.0
Error: There is not enough information to infer relation "__public__.courses.dependentCourses"
at normalizeRelation [...]
at Command.<anonymous> [...]

Node.js v21.4.0
2 Replies
JustKira
JustKiraOP11mo ago
Schema
export const courses = mysqlTable("courses", {
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-${createId()}`),
name: varchar("name", { length: 128 }).unique().notNull(),
description: text("description"),
});

export const courseRelations = relations(courses, ({ many }) => ({
nodes: many(nodes),
dependentCourses: many(course_dependencies, {
relationName: "depended_courses",
}),
courseDependencies: many(course_dependencies, {
relationName: "course_dependencies",
}),
}));

export const course_dependencies = mysqlTable(
"course_dependencies",
{
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-dep-${createId()}`),
courseId: varchar("courseId", { length: 128 }).notNull(),
dependencyCourseId: varchar("dependencyCourseId", {
length: 128,
}).notNull(),
},
(t) => ({
unq: unique().on(t.dependencyCourseId, t.courseId),
})
);

export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
}),
})
);
export const courses = mysqlTable("courses", {
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-${createId()}`),
name: varchar("name", { length: 128 }).unique().notNull(),
description: text("description"),
});

export const courseRelations = relations(courses, ({ many }) => ({
nodes: many(nodes),
dependentCourses: many(course_dependencies, {
relationName: "depended_courses",
}),
courseDependencies: many(course_dependencies, {
relationName: "course_dependencies",
}),
}));

export const course_dependencies = mysqlTable(
"course_dependencies",
{
id: varchar("id", { length: 128 })
.primaryKey()
.$defaultFn(() => `cou-dep-${createId()}`),
courseId: varchar("courseId", { length: 128 }).notNull(),
dependencyCourseId: varchar("dependencyCourseId", {
length: 128,
}).notNull(),
},
(t) => ({
unq: unique().on(t.dependencyCourseId, t.courseId),
})
);

export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
}),
})
);
Angelelz
Angelelz11mo ago
You need to use the relationName on the other side of the relation so that drizzle can connect the two:
export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
relationName: "course_dependencies"
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
relationName: "depended_courses"
}),
})
);
export const course_dependenciesRelations = relations(
course_dependencies,
({ one }) => ({
course: one(courses, {
fields: [course_dependencies.courseId],
references: [courses.id],
relationName: "course_dependencies"
}),
courseDependency: one(courses, {
fields: [course_dependencies.dependencyCourseId],
references: [courses.id],
relationName: "depended_courses"
}),
})
);
You might need to switch the name, just test it a check if it gives you the correct result
Want results from more Discord servers?
Add your server