Repository navigation
Prisma relations not updating related objects #19070
|
I'm building a Next.js and Nest.js project with Prisma as my ORM, and I'm encountering an issue with the way Prisma manages relationships between my User and Project models. Specifically, when I create a new Project and associate it with one or more User objects, the Project is successfully created with the correct user data, but the corresponding User objects do not appear to be updated with the new project information in their projects array. Here's an excerpt of my Prisma schema for reference: model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
//...
projects Project[] @relation(name: "users", fields: [project_ids], references: [id])
project_ids String[] @db.ObjectId
createdProjects Project[] @relation(name: "pmId")
//...
@@map(name: "users")
}
model Project {
id String @id @default(auto()) @map("_id") @db.ObjectId
//...
users User[] @relation(name: "users", fields: [user_ids], references: [id])
user_ids String[] @db.ObjectId
pm User? @relation(name: "pmId", fields: [pmId], references: [id], onDelete: Cascade)
pmId String? @db.ObjectId
//...
@@map(name: "projects")
}Service: Controller: However, when I check the projects array of each User object, it doesn't seem to have been updated with the new project information. I've tried various debugging steps, including checking my Prisma schema, verifying my database backend (MongoDB) configuration, and using Prisma's debugging features, but I can't seem to identify the issue. Any help or guidance on how to resolve this issue would be greatly appreciated. Thanks in advance! |
Replies: 1 comment 7 replies
|
Hi @Dedpy 👋 Can you please share the query you are performing? |

@ludralph Thank you very much for offering your help; it is much appreciated.
I am trying to create a project where I need to connect it with existing users by passing their IDs in the data.
However, when I attempt to remove the
Prisma.ProjectCreateInputand replace it withany, it worked perfectly.While this approach worked, I found that ProjectCreateInput does not include the user_ids array. I also attempted to use
Prisma.ProjectUncheckedCreateInput, but it resulted in errors when using the map functions.This is what I went with as a solution: