0

I created a document and tried reading the document. It was working fine. Then, I updated the same document and tried to read it.

Prisma is returning null eventhough data is present in MongoDB.

Prisma Models:

model User {
 id String @id @default(auto()) @map("_id") @db.ObjectId
 name String
 email String @unique
 ... other fields
 
 posts Post[]
 teamMember TeamMember[]
}

model Post {
 id String @id @default(auto()) @map("_id") @db.ObjectId
 title String
 ...Other fields

 teamMembers TeamMember[]
}

model TeamMember {
 id String @id @default(auto()) @map("_id") @db.ObjectId
 ...other fields

 post   Post? @relation(fields: [postId], references: [id], onDelete: Cascade)
 postId String? @db.ObjectId

 member User? @relation(fields: [memberId], referenced: [id])
 memberId String @db.ObjectId
}


Prisma Query to Update Post:

const updatePost = await prisma.post.update({
 where:{
  id: postId
 },
 data:{
  title: data.title,
  ...other fields
  teamMembers:{
   deleteMany: {},
   createMany: data.teamMembers
  }
 }
})

Error:

const post = await prisma.post.findUnique(
Inconsistent query result: Field member is required to return data, got `null` instead.

Please assist me. Thanks.

  • Does this answer your question? [Create or update one to many relationship in Prisma](https://stackoverflow.com/questions/68285222/create-or-update-one-to-many-relationship-in-prisma) – some-user Oct 04 '22 at 12:39
  • @some-user, I was able to delete the existing array of objects (team members) and create them back with new array of object. But, after update, Prisma returns null when trying to read it. – Ajitesh Sivakumar Oct 04 '22 at 13:07
  • I guess, you have to `include` it to get it back - as you would in a `findMany`/`findFirst`/.... – some-user Oct 04 '22 at 13:13
  • I have already used nested select with `findUnique`. The point is `findUnique` / `findMany` is **working before document update**, but **not after update**. Any ideas? – Ajitesh Sivakumar Oct 04 '22 at 13:23
  • Can you provide a reproduction? – some-user Oct 04 '22 at 13:30
  • Can you add the findUnique query in the question so that I can try to reproduce it? – Nurul Sundarani Oct 04 '22 at 14:32

0 Answers0