One to Many connection not working correctly in Dirzzle

So i am trying to get a one to many relation working with drizzle and it keeping this error: Error: There is not enough information to infer relation "recipes.ingredients" this is my relation for the recipes these are all my relations for the recipe
// recipes relation
export const recipesRelation = relations(recipes, ({ one, many }) => ({
ingredients: many(ingredients),
info: one(info, {
fields: [recipes.uid],
references: [info.recipeId],
}),
ratings: one(ratings, {
fields: [recipes.uid],
references: [ratings.recipeId],
}),
}));

export const ingredientsRelation = relations(recipes, ({ one } => ({
ingredients: one(ingredients, {
fields: [recipes.uid],
references: [ingredients.recipeId],
}),
}));

export const infoRelations = relations(recipes, ({ one }) => ({
info: one(info, {
fields: [recipes.uid],
references: [info.recipeId],
}),
}));

export const ratings = mysqlTable('ratings', {
recipeId: char('recipeId', { length: 36 }).primaryKey().notNull(),
userId: varchar('userId', { length: 191 }).notNull(),
rating: tinyint('rating').notNull(),
});
// recipes relation
export const recipesRelation = relations(recipes, ({ one, many }) => ({
ingredients: many(ingredients),
info: one(info, {
fields: [recipes.uid],
references: [info.recipeId],
}),
ratings: one(ratings, {
fields: [recipes.uid],
references: [ratings.recipeId],
}),
}));

export const ingredientsRelation = relations(recipes, ({ one } => ({
ingredients: one(ingredients, {
fields: [recipes.uid],
references: [ingredients.recipeId],
}),
}));

export const infoRelations = relations(recipes, ({ one }) => ({
info: one(info, {
fields: [recipes.uid],
references: [info.recipeId],
}),
}));

export const ratings = mysqlTable('ratings', {
recipeId: char('recipeId', { length: 36 }).primaryKey().notNull(),
userId: varchar('userId', { length: 191 }).notNull(),
rating: tinyint('rating').notNull(),
});
this is the query i am running
const recipe = await db.query.recipes.findMany({
with: {
info: true,
ingredients: true,
ratings: true,
}
});
const recipe = await db.query.recipes.findMany({
with: {
info: true,
ingredients: true,
ratings: true,
}
});
when i run it without the ingredients: true it works and gets the info so not really sure why the many(ingredients) is not working
2 Replies
meeepsss
meeepsss16mo ago
Snow us the schema?
Jacob
Jacob16mo ago
this is my schema for the recipes
export const recipes = mysqlTable('recipes', {
uid: char('uid', { length: 36 }).primaryKey().notNull(),
id: varchar('id', { length: 191 }).notNull(),
title: varchar('title', { length: 191 }).notNull(),
description: text('description'),
imgUrl: text('imgUrl'),
imgAlt: text('imgAlt'),
notes: text('notes'),
steps: json('steps'),
mealTime: mealTime.default('not_set'),
version: tinytext('version').notNull().default('1.0.0'),

// little extras for searching
keywords: json('keywords'),
dietary: dietary.default('none'),
allergens: json('allergens'),
sweet_savoury: sweet_savoury.default('not_set'),
difficulty_level: difficulty_level.default('not_set'),
cuisine: json('cuisine'),
isPublic: boolean('isPublic').notNull().default(false),

// users
createdAt: timestamp('createdAt').notNull().defaultNow(),
lastUpdated: timestamp('lastUpdated').notNull().defaultNow().onUpdateNow(),
lastUpdatedBy: varchar('lastUpdatedBy', { length: 191 }).notNull(),
createdBy: varchar('createdBy', { length: 191 }).notNull(),

madeRecipe: int('madeRecipe'),
savedRecipe: int('savedRecipe'),
});

export const ingredients = mysqlTable('ingredients', {
recipeId: char('recipeId', { length: 36 }).notNull(),
isHeading: boolean('isHeading').notNull(),
title: varchar('title', { length: 191 }),
unit: unit.default('not_set'),
quantity: int('quantity'),
amount: amount.default('not_set'),
});

export const ratings = mysqlTable('ratings', {
recipeId: char('recipeId', { length: 36 }).primaryKey().notNull(),
userId: varchar('userId', { length: 191 }).notNull(),
rating: tinyint('rating').notNull(),
});
export const recipes = mysqlTable('recipes', {
uid: char('uid', { length: 36 }).primaryKey().notNull(),
id: varchar('id', { length: 191 }).notNull(),
title: varchar('title', { length: 191 }).notNull(),
description: text('description'),
imgUrl: text('imgUrl'),
imgAlt: text('imgAlt'),
notes: text('notes'),
steps: json('steps'),
mealTime: mealTime.default('not_set'),
version: tinytext('version').notNull().default('1.0.0'),

// little extras for searching
keywords: json('keywords'),
dietary: dietary.default('none'),
allergens: json('allergens'),
sweet_savoury: sweet_savoury.default('not_set'),
difficulty_level: difficulty_level.default('not_set'),
cuisine: json('cuisine'),
isPublic: boolean('isPublic').notNull().default(false),

// users
createdAt: timestamp('createdAt').notNull().defaultNow(),
lastUpdated: timestamp('lastUpdated').notNull().defaultNow().onUpdateNow(),
lastUpdatedBy: varchar('lastUpdatedBy', { length: 191 }).notNull(),
createdBy: varchar('createdBy', { length: 191 }).notNull(),

madeRecipe: int('madeRecipe'),
savedRecipe: int('savedRecipe'),
});

export const ingredients = mysqlTable('ingredients', {
recipeId: char('recipeId', { length: 36 }).notNull(),
isHeading: boolean('isHeading').notNull(),
title: varchar('title', { length: 191 }),
unit: unit.default('not_set'),
quantity: int('quantity'),
amount: amount.default('not_set'),
});

export const ratings = mysqlTable('ratings', {
recipeId: char('recipeId', { length: 36 }).primaryKey().notNull(),
userId: varchar('userId', { length: 191 }).notNull(),
rating: tinyint('rating').notNull(),
});
Want results from more Discord servers?
Add your server