when I update a record whose model has connected f...
# orm-help
d
when I update a record whose model has connected fields, do i have to explicitly map those to the referenced name in the relation?
Copy code
.update({
  data: {
          ...updateData,
          origin: {
            connect: {
              city: origin
            },
          },
        },
  where: {id},
}
r
@Davedavedave 👋 I couldn’t quite get your question, could you elaborate a bit on this?
d
yeah i guess i phrased it poorly, sorry. i have this case when creating a record https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#connect-an-existing-record which works fine. so I am wondering if I have to update a record of this type, do i have to update it via the same syntax
but i guess the lower part of that explanation shows it
r
So you want to update the relation record or do you want to remove that and connect a new one?
d
yeah i would like to update the record to point to a different record of the same relation
in this case u have to use the connect syntax again?
r
Yes to add a record you can use
connect
and to remove a record you can use
disconnect
.
d
so in my case i have like 5 relations that could possibly get updated in my request. Which means i need to write a mapper to check if the field is present and then generate a list of entries like this
Copy code
fieldname: {
  connect: {
   nameOfRelationField: value
  }
}
and append these to the provided data of update
I am only wondering because this seems kinda cumbersome and thought I miss something obvious
I guess it would be easier to always send the complete object from the frontend and always update the every field of the record
r
Is there an example that you have along with your schema? I could suggest a better option if there’s one.