Hey all how can I update many records where I have...
# orm-help
n
Hey all how can I update many records where I have the userId + default = true, schema shared in the thread. thanks in advance
Copy code
model Address {
  id             String        @id @default(uuid())
  createdAt      DateTime      @default(now())
  updatedAt      DateTime      @updatedAt
  type           AddressType
  lineOne        String
  lineTwo        String?
  lineThree      String?
  city           String
  state          String
  postalCode     String
  country        String
  notes          String?
  default        Boolean       @default(false)
  userId         String?
  user           User?         @relation(fields: [userId], references: [id])
  organizationId String?
  organization   Organization? @relation(fields: [organizationId], references: [id])

  @@index([id, userId])
  @@index([id, userId, default])
  @@index([id, organizationId])
  @@index([id, organizationId, default])
}
Use case: I want to update address records where I have either the userId or organizationId and default
Copy code
await this.prisma.address.update({
          where: {
            userId: createPayload.userId,
            default: true
          }
        })
a
Do you want to make the same update to all of the records?
Like, change the city/state/postalCode to the same exact values for all of the addresses you're updating?
in that case, updateMany would probably work
n
i just want to update the users default address in this case, however in another case
updateMany works 👍, but let's say I want to update an individual address with only the
userId
and the
default
status how can i achieve that
a
i'd probably do two queries -- first a query with a findOne, to get the address. and then do a subsequent update now that you have the address and ID
alternatively, if it's unique you could query the user and include the defaultAddress relation, but taht's still a query to find and a follow-up query to update, so it's not necessarily any better.
1
n
thanks @Austin Zentz
👍 1