Studio: Not Enough Info to Infer Relation

https://www.answeroverflow.com/m/1180072254214766632 Running into this issue:
bun run drizzle-kit studio
Error: There is not enough information to infer relation "__public__.auditLogs.users"
bun run drizzle-kit studio
Error: There is not enough information to infer relation "__public__.auditLogs.users"
export const users = sqliteTable('users', {
id: text('id').$default(() => createId()).primaryKey(),
username: text('username').unique().notNull(),
email: text('email'),
//...
});

export const usersRelations = relations(users, ({ one, many }) => ({
auditLogs: many(auditLogs),
//...
}));

export const auditLogs = sqliteTable('audit_logs', {
userId: text('user_id').references(() => users.id, { onDelete: 'cascade' }),
action: text('action'),
timeStamp: text('time_stamp').$default(() => toISO8601(new Date())),
}, (table) => ({
unq: unique().on(table.userId, table.action, table.timeStamp),
}));

export const auditLogsRelations = relations(auditLogs, ({ one }) => ({
users: one(users),
}));
export const users = sqliteTable('users', {
id: text('id').$default(() => createId()).primaryKey(),
username: text('username').unique().notNull(),
email: text('email'),
//...
});

export const usersRelations = relations(users, ({ one, many }) => ({
auditLogs: many(auditLogs),
//...
}));

export const auditLogs = sqliteTable('audit_logs', {
userId: text('user_id').references(() => users.id, { onDelete: 'cascade' }),
action: text('action'),
timeStamp: text('time_stamp').$default(() => toISO8601(new Date())),
}, (table) => ({
unq: unique().on(table.userId, table.action, table.timeStamp),
}));

export const auditLogsRelations = relations(auditLogs, ({ one }) => ({
users: one(users),
}));
Any ideas? Based on the stack overflow, the suggestion is that the relationship isn't on both sides, but it seems like I do in this case, no?
There's not enough information to infer relation - Drizzle Team
I am trying to use drizzle studio but I'm running into the following issue, There is not enough information to infer relation "__public__.collectionsTable.tokens". Here's a simplified version of the schema. ```ts export const tokensTable = mysqlTable( 'tokens', { tokenId: varchar('token_id', { length: 255 }).notNul...
4 Replies
Mykhailo
Mykhailo9mo ago
Hello, @stephen! You have to update your auditLogsRelations with this
user: one(users, { fields: [auditLogs.userId], references: [users.id] }),
user: one(users, { fields: [auditLogs.userId], references: [users.id] }),
stephen
stephenOP9mo ago
thanks @solo ! i think i've got dozens of these relations configured incorrectly it seems... can you point me to the docs where i might learn more about this second argument?
Sillvva
Sillvva9mo ago
The relations docs show examples https://orm.drizzle.team/docs/rqb#declaring-relations And the foreign key docs explain the similarities and differences https://orm.drizzle.team/docs/rqb#foreign-keys
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
stephen
stephenOP9mo ago
Thanks! Now I'm trying to follow the pattern for a many-to-many and am getting stuck.
export const menuCourses = sqliteTable('menu_courses', {
id: text('id').$default(() => createId()).primaryKey(),
menuId: text('menu_id').references(() => menus.id, { onDelete: "cascade" }).notNull(),
name: text('name'),
});

export const menuCoursesRelations = relations(menuCourses, ({ one, many }) => ({
menu: one(menus, { fields: [menuCourses.menuId], references: [menus.id] }),
menuCourseRecipes: many(menuRecipes),
}));

export const menuRecipes = sqliteTable('menu_recipes', {
id: text('id').$default(() => createId()).primaryKey(),
menuId: text('menu_id').references(() => menus.id, { onDelete: "cascade" }).notNull(),
// recipeId: text('recipe_id').references(() => recipes.id, { onDelete: "cascade" }).notNull(),
menuCourseId: text('menu_course_id').references(() => menuCourses.id, { onUpdate: "cascade" }),
});

export const menuRecipesRelations = relations(menuRecipes, ({ one }) => ({
menuCourse: one(menuCourses),
}));

export const menuCourseRecipes = sqliteTable('menu_course_recipes', {
menuCourseId: text('menu_course_id').references(() => menuCourses.id, { onDelete: "cascade" }).notNull(),
menuRecipeId: text('recipe_id').references(() => menuRecipes.id, { onDelete: "cascade" }).notNull(),
}, (t) => ({
pk: primaryKey({ columns: [t.menuCourseId, t.menuRecipeId] }),
}));

export const menuCourseRecipesRelations = relations(menuCourseRecipes, ({ one }) => ({
menuCourses: one(menuCourses, {
fields: [menuCourseRecipes.menuCourseId],
references: [menuCourses.id],
}),
menuRecipes: one(menuRecipes, {
fields: [menuCourseRecipes.menuRecipeId],
references: [menuRecipes.id],
}),
}));
export const menuCourses = sqliteTable('menu_courses', {
id: text('id').$default(() => createId()).primaryKey(),
menuId: text('menu_id').references(() => menus.id, { onDelete: "cascade" }).notNull(),
name: text('name'),
});

export const menuCoursesRelations = relations(menuCourses, ({ one, many }) => ({
menu: one(menus, { fields: [menuCourses.menuId], references: [menus.id] }),
menuCourseRecipes: many(menuRecipes),
}));

export const menuRecipes = sqliteTable('menu_recipes', {
id: text('id').$default(() => createId()).primaryKey(),
menuId: text('menu_id').references(() => menus.id, { onDelete: "cascade" }).notNull(),
// recipeId: text('recipe_id').references(() => recipes.id, { onDelete: "cascade" }).notNull(),
menuCourseId: text('menu_course_id').references(() => menuCourses.id, { onUpdate: "cascade" }),
});

export const menuRecipesRelations = relations(menuRecipes, ({ one }) => ({
menuCourse: one(menuCourses),
}));

export const menuCourseRecipes = sqliteTable('menu_course_recipes', {
menuCourseId: text('menu_course_id').references(() => menuCourses.id, { onDelete: "cascade" }).notNull(),
menuRecipeId: text('recipe_id').references(() => menuRecipes.id, { onDelete: "cascade" }).notNull(),
}, (t) => ({
pk: primaryKey({ columns: [t.menuCourseId, t.menuRecipeId] }),
}));

export const menuCourseRecipesRelations = relations(menuCourseRecipes, ({ one }) => ({
menuCourses: one(menuCourses, {
fields: [menuCourseRecipes.menuCourseId],
references: [menuCourses.id],
}),
menuRecipes: one(menuRecipes, {
fields: [menuCourseRecipes.menuRecipeId],
references: [menuRecipes.id],
}),
}));
/Users/stephenweiss/code/plated/node_modules/drizzle-orm/relations.cjs:261
throw new Error(
^

Error: There is not enough information to infer relation "__public__.menuRecipes.menu"
at normalizeRelation (/Users/stephenweiss/code/plated/node_modules/drizzle-orm/relations.cjs:261:9)
at /Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:73:67
at Array.map (<anonymous>)
at /Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:72:42
at Array.map (<anonymous>)
at extractRelations (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:71:51)
at sqliteQueryEngine (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:293:16)
at queryEngineForSetup (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:716:12)
at Command.<anonymous> (/Users/stephenweiss/code/plated/node_modules/drizzle-kit/bin.cjs:63755:62)

Node.js v20.11.1
error: script "db:studio" exited with code 1
/Users/stephenweiss/code/plated/node_modules/drizzle-orm/relations.cjs:261
throw new Error(
^

Error: There is not enough information to infer relation "__public__.menuRecipes.menu"
at normalizeRelation (/Users/stephenweiss/code/plated/node_modules/drizzle-orm/relations.cjs:261:9)
at /Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:73:67
at Array.map (<anonymous>)
at /Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:72:42
at Array.map (<anonymous>)
at extractRelations (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:71:51)
at sqliteQueryEngine (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:293:16)
at queryEngineForSetup (/Users/stephenweiss/code/plated/node_modules/@drizzle-team/studio/index.js:716:12)
at Command.<anonymous> (/Users/stephenweiss/code/plated/node_modules/drizzle-kit/bin.cjs:63755:62)

Node.js v20.11.1
error: script "db:studio" exited with code 1
jk i think i see it 🤦‍♀️ yup! i got it! 😅 (I was looking at the wrong table's relations ... 😬 )
Want results from more Discord servers?
Add your server