#guys can someone help with a one to many issue.

7 messages · Page 1 of 1 (latest)

steady timber
#

/// these are the models

model Menu {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())

menuName String
isPdfPublic Boolean
// menuCategories MenuCategory[]
menuPdf MenuPdf[]

menuCategories MenuCategory[]

entity Entity @relation(fields: [entityId], references: [id])
entityId String @unique
}

model MenuCategory {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())

categoryName String
categoryPublic Boolean

// categoryTag String // the category tag is like drinks/food/inventory/perishables...

// menu id
menuId Int @unique
Menu Menu @relation(fields: [menuId], references: [id])

menuItem MenuItem[]
}

///this is the addModelFunction'

export async function addCategory({
menuId,
categoryName,
categoryPublic=false,
}:AddCategory) {

try {
const category = await prisma?.menuCategory.create({
data: {
menuId,
categoryName,
categoryPublic: false,
}
});

console.log("category from add category", category);

return category;

} catch (error) {
throw new Error(the error is from addCategory ${error})
// console.log('error', error);
}
}

////this is the error

Invalid prisma.menuCategory.create() invocation:

Unique constraint failed on the constraint: MenuCategory_menuId_key
at addCategory (./src/lib/entity/entityCategories.ts:30:15)
32 | return category;
33 | } catch (error) {

34 | throw new Error(the error is from addCategory ${error})
| ^

woven brook
#

@steady timber first, using prisma should be optionally chained, with a question mark.
Second, you orobably tryzing to add something which already exists in the database, since unique means that you can have only one of that id in the db

steady timber
#

thank you

#

i ended up rewriting the whole model.

#

the foreign key i thought should be unique but that turned out to be the issue

#

i removed the @unique pa and added an index for the error and it ended up working.

#

spent 2 days on docs man