Drizzle studio : Many To Many : There is not enough information to infer relation

I can't seen to get rid of the following error with Drizzle studio :
Error: There is not enough information to infer relation "__public__.course.recommendations"
Error: There is not enough information to infer relation "__public__.course.recommendations"
Here are the relevant parts of my schema.
export const course = pgTable('Course', {
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
name: text('name').notNull(),
description: text('description').notNull(),
ordinal: integer('ordinal').notNull(),
duration: integer('duration').default(0).notNull(),
thumbnails: text('thumbnails').array(),
tags: text('tags').array(),
supportingMedias: text('supportingMedias').array(),
active: integer('active').default(0).notNull(),
previewVideo: text('previewVideo'),
downloadMediaZip: text('downloadMediaZip'),
rating: numericNumber('rating', { precision: 65, scale: 30 }).default(0).notNull(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
});

export const courseRelations = relations(course, ({ one, many }) => ({
// 1 - N
user: one(user, { fields: [course.userId], references: [user.id] }),
// M - N
bookmarks: many(bookmark),
chapters: many(chapter, { relationName: 'chaptersCourse' }),
comments: many(comment),
courseSubscriptions: many(courseSubscription),
likes: many(like),
notes: many(note),
ratingsReviews: many(ratingsReview),
videoLogs: many(videoLog),
// M - M
recommendations: many(recommendation)
}));


export const recommendation = pgTable(
'Recommendation',
{
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
tags: text('tags').default('{}').array(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
},
(table) => {
return {
userIdKey: uniqueIndex('Recommendation_userId_key').on(table.userId)
};
}
);

export const recommendationRelations = relations(recommendation, ({ one, many }) => ({
user: one(user, { fields: [recommendation.userId], references: [user.id] }),
courses: many(course),
interests: many(interest)
}));

/**
* Many to Many
*/

export const recommendationsToCourses = pgTable(
'Recommendations_to_Courses',
{
recommendationId: text('recommendationId')
.notNull()
.references(() => recommendation.id, cascade),
courseId: text('courseId')
.notNull()
.references(() => course.id, cascade)
},
(t) => ({ pk: primaryKey({ columns: [t.recommendationId, t.courseId] }) })
);

export const recommendationsToCoursesRelations = relations(recommendationsToCourses, ({ one }) => ({
course: one(course, {
fields: [recommendationsToCourses.courseId],
references: [course.id]
}),
recommendation: one(recommendation, {
fields: [recommendationsToCourses.recommendationId],
references: [recommendation.id]
})
}));
export const course = pgTable('Course', {
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
name: text('name').notNull(),
description: text('description').notNull(),
ordinal: integer('ordinal').notNull(),
duration: integer('duration').default(0).notNull(),
thumbnails: text('thumbnails').array(),
tags: text('tags').array(),
supportingMedias: text('supportingMedias').array(),
active: integer('active').default(0).notNull(),
previewVideo: text('previewVideo'),
downloadMediaZip: text('downloadMediaZip'),
rating: numericNumber('rating', { precision: 65, scale: 30 }).default(0).notNull(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
});

export const courseRelations = relations(course, ({ one, many }) => ({
// 1 - N
user: one(user, { fields: [course.userId], references: [user.id] }),
// M - N
bookmarks: many(bookmark),
chapters: many(chapter, { relationName: 'chaptersCourse' }),
comments: many(comment),
courseSubscriptions: many(courseSubscription),
likes: many(like),
notes: many(note),
ratingsReviews: many(ratingsReview),
videoLogs: many(videoLog),
// M - M
recommendations: many(recommendation)
}));


export const recommendation = pgTable(
'Recommendation',
{
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
tags: text('tags').default('{}').array(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
},
(table) => {
return {
userIdKey: uniqueIndex('Recommendation_userId_key').on(table.userId)
};
}
);

export const recommendationRelations = relations(recommendation, ({ one, many }) => ({
user: one(user, { fields: [recommendation.userId], references: [user.id] }),
courses: many(course),
interests: many(interest)
}));

/**
* Many to Many
*/

export const recommendationsToCourses = pgTable(
'Recommendations_to_Courses',
{
recommendationId: text('recommendationId')
.notNull()
.references(() => recommendation.id, cascade),
courseId: text('courseId')
.notNull()
.references(() => course.id, cascade)
},
(t) => ({ pk: primaryKey({ columns: [t.recommendationId, t.courseId] }) })
);

export const recommendationsToCoursesRelations = relations(recommendationsToCourses, ({ one }) => ({
course: one(course, {
fields: [recommendationsToCourses.courseId],
references: [course.id]
}),
recommendation: one(recommendation, {
fields: [recommendationsToCourses.recommendationId],
references: [recommendation.id]
})
}));
I tried adding a relationName with the name of the pivot table like this
// RecommendationRelations
courses: many(course, { relationName: 'Recommendations_to_Courses' }),

// CourseRelations
recommendations: many(recommendation, { relationName: 'Recommendations_to_Courses' })
// RecommendationRelations
courses: many(course, { relationName: 'Recommendations_to_Courses' }),

// CourseRelations
recommendations: many(recommendation, { relationName: 'Recommendations_to_Courses' })
But that did not change anything. Am I missing something ? I've seen similar questions previously asked, but none of the suggestions seems to apply for my specific case.
6 Replies
Angelelz
Angelelz11mo ago
You're debuggings steps should start at what query is throwing the error, and then figure out where is the missing relation
Hebilicious
HebiliciousOP11mo ago
facepalm I misread the documentation, you have to define the relation on the table with the pivot table, not the table tableA has many PivotTableAB not tableA has many TableB
Angelelz
Angelelz11mo ago
Good deal!
Hebilicious
HebiliciousOP11mo ago
slick. Is the querying within studio supposed to work though ? Think suspect this is a bug due to my uuid columns not being detected correctly by studio ?
export const uuid = () => sql`uuid_generate_v4()`;

export const achievementEvent = pgTable('AchievementEvent', {
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
// deletedAt: timestamp('deletedAt', { precision: 3, mode: 'string' }),
name: text('name').notNull(),
event: text('event').notNull(),
type: text('type').notNull(),
rP: integer('rP').default(0).notNull()
});
export const uuid = () => sql`uuid_generate_v4()`;

export const achievementEvent = pgTable('AchievementEvent', {
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
// deletedAt: timestamp('deletedAt', { precision: 3, mode: 'string' }),
name: text('name').notNull(),
event: text('event').notNull(),
type: text('type').notNull(),
rP: integer('rP').default(0).notNull()
});
The actual schema code looks like this
Angelelz
Angelelz11mo ago
That's probably it, if you can edit it you can wrap the stuff in defaul with a sql like this:
sql`uuid_generate_v4()`
sql`uuid_generate_v4()`
I'm not a drizzle studio user tbh
Hebilicious
HebiliciousOP11mo ago
Doesn't look like the schema is editable, I'll submit an issue on gh <a:thumbs_up:1019821758083256390> Thanks for your time
Want results from more Discord servers?
Add your server