prisma-client-go icon indicating copy to clipboard operation
prisma-client-go copied to clipboard

Running an update after FindMany fails with a FieldNotFoundError when FindMany gives back an empty result

Open koenigsascha opened this issue 3 years ago • 6 comments

Running an update after FindMany fails with a FieldNotFoundError when FindMany gives back an empty result, BUT only when I want to update a foreign key field.

Error in query graph construction: QueryParserError(ObjectValidationError { object_name: "Mutation", inner: FieldValidationError { field_name: "updateManyNetwork", inner: ArgumentValidationError { argument: "data", inner: ObjectValidationError { object_name: "NetworkUpdateManyMutationInput", inner: FieldValidationError { field_name: "owner", inner: FieldNotFoundError } } } } })

example:

_, err := r.Client.Network.FindMany(
	db.Network.ID.Equals("ckfxy9dh00003c5rp97l1cica"),
        db.Network.UpdatedAt.Before(timestamp),
).Update(
	db.Network.Owner.Link(db.User.ID.Equals("ckfxy02u30002c5rpvlxchzx8")),  // foreign key field; update fails
        db.Network.OwnerID.Set("ckfxy02u30002c5rpvlxchzx8"), // tried this alternatively; same error
        db.Network.Name.Set("some name"), // normal field; no problem when findMany does not find anything.
).Exec(ctx)

relevant part of the model:

model Network {
  id                String            @id @default(cuid())
  owner             User              @relation(fields: [ownerId], references: [id])
  ownerId           String
  name              String
}

koenigsascha avatar Oct 09 '20 08:10 koenigsascha

Did you make sure to go run github.com/prisma/prisma-client-go again? If yes, can you please set export DEBUG=* and export PHOTON_GO_LOG=info and share the logs here (or at [email protected])?

steebchen avatar Oct 09 '20 13:10 steebchen

It looks like the Prisma query engine doesn't allow setting foreign keys (linking records) in FindMany, while it is possible in FindOne. I'll investigate further and will report back.

For a temporary solution, you might be able to use FindOne if it's possible to exclude the UpdatedAt query with your use case.

steebchen avatar Oct 09 '20 16:10 steebchen

It's indeed something the query engine doesn't support at this time. I've created an issue at prisma/prisma.

steebchen avatar Oct 09 '20 16:10 steebchen

Thank you!

koenigsascha avatar Oct 09 '20 18:10 koenigsascha

I have re-opened this as a feature request. For now, it will depend on https://github.com/prisma/prisma/issues/3887.

steebchen avatar Oct 15 '20 15:10 steebchen

This now depends on https://github.com/prisma/prisma-client-go/issues/308. When that is done, we need add a constraint to Link/Unlink to not accept this payload. Instead, the user should use unchecked scalar writes.

This would look as follows when taking the original example (note that this doesn't work at this time):

_, err := r.Client.Network.FindMany(
	db.Network.ID.Equals("ckfxy9dh00003c5rp97l1cica"),
    db.Network.UpdatedAt.Before(timestamp),
).Update(
   db.Network.OwnerID.Set("ckfxy02u30002c5rpvlxchzx8"),
   db.Network.Name.Set("some name"),
).Exec(ctx)

steebchen avatar Jan 19 '21 17:01 steebchen

depends on #401 now

steebchen avatar Jun 10 '23 20:06 steebchen