Home > Back-end >  Prisma - "not assignable to type" error" on trying to update a field
Prisma - "not assignable to type" error" on trying to update a field

Time:08-18

I'm pretty new to Prisma. I'm trying to update a field based on a foreign key. but it gives me the following error:

Type '{ authorId: number; }' is not assignable to type 'PatientWhereUniqueInput'.

My schema is like this:

  id Int @id @default(autoincrement())
  name String?
  password String
  email String @unique
  patient Patient[]
}

model Patient {
  id Int @id @default(autoincrement())
  firstName String
  lastName String
  email String @unique
  password String
  contact String
  address String
  dob String
  image String?
  author User @relation(fields: [authorId], references: [id])
  authorId Int
  specialAttention Boolean @default(false)
}

This is how I'm trying to update, where userId is the currently logged in user's Id from the middleware.

  const updatedPatient = await prisma.patient.update({
    where: {
      authorId: userId,
    },
    data: body,
  });
  return updatedPatient;
};

CodePudding user response:

you can only update through the unique fields id and email.

type PatientWhereUniqueInput {
  id?: Int
  email?: String
}

i.e your query should be structured like

const updatedPatient = await prisma.patient.update({
    where: {
      email: userEmail,
    },
    data: body,
  });
  return updatedPatient;
};

but to be able to perform the update on any of the fields, you can use updateMany. For example:

const updatedPatient = await prisma.patient.updateMany({
  where: {
    authorId: 1 ,
  },
  data: {
    firstName: 'William'
  },
});

Also you can find a similar issue where this was discussed here

  • Related