how would you handle this scenario? i have a clien...
# orm-help
l
how would you handle this scenario? i have a client table that can have several addresses, what should i do with the approach at the time of update, Scenario : if i remove an address from the array is the update capabilities of Prisma prepared for that one? is there any example i can check on something like that?
r
@Luis Haroldo Castro Cabrera 👋 How are the addresses stored?
l
hi, they are in a separate table with type (they could be home, work, etc.., the relation will be 1 to many against the client. the save was quite easy i guess but I'm struggling a little bit to imagine how an update will be using prisma, with plain SQL it will be straight forward ( but more work I guess). can show the schema if need
r
Sure it would be great if you could share the schema.
l
Copy code
model Cliente {
    id                   String         @id @default(uuid())
    nombre               String
    apellidoPaterno      String
    apellidoMaterno      String
    fechaDeNacimiento    DateTime
    rfc                  String         @unique
    curp                 String         @unique
    telefono1            String
    telefono2            String?
    trabajo              Trabajo        @relation(fields: [trabajoId], references: [id])
    estadoCivil          EstadoCivil    @relation(fields: [estadoCivilId], references: [id])
    tipoDeVivienda       TipoDeVivienda @relation(fields: [tipoDeViviendaId], references: [id])
    escolaridad          Escolaridad    @relation(fields: [escolaridadId], references: [id])
    genero               Genero         @relation(fields: [generoId], references: [id])
    direcciones          Direccion[]
    creditos             Credito[]
    montoMinimo          Decimal        @db.Decimal(15, 2)
    montoMaximo          Decimal        @db.Decimal(15, 2)
    numeroCreditosCrecer Int            @default(3)
    estadoCivilId        String
    tipoDeViviendaId     String
    escolaridadId        String
    generoId             String
    trabajoId            String
    creadoPor            String
    fechaCreacion        DateTime       @default(now())
    actualizadoPor       String         @default("ADMIN")
    fechaActualizacion   DateTime       @default(now()) @updatedAt

    @@map(name: "Clientes")
}
that's client and
Copy code
model Direccion {
    id                 String        @id @default(uuid())
    tipo               TipoDireccion
    calle              String
    numero             String
    cruzamientos       String?
    colonia            Colonia       @relation(fields: [coloniaId], references: [id])
    sucursales         Sucursal[]
    cliente            Cliente?      @relation(fields: [clienteId], references: [id])
    creadoPor          String
    fechaCreacion      DateTime      @default(now())
    actualizadoPor     String        @default("ADMIN")
    fechaActualizacion DateTime      @default(now()) @updatedAt
    clienteId          String?
    coloniaId          String

    Trabajo Trabajo[]
    @@map(name: "Direcciones")
}
that will be Address (direccion in spanish)
r
I simplified your schema to look like this:
Copy code
model User {
  id        Int       @id @default(autoincrement())
  name      String
  addresses Address[]
}

model Address {
  id     Int    @id @default(autoincrement())
  line1  String
  user   User?  @relation(fields: [userId], references: [id])
  userId Int?
}
Updating a specific address of a User is easy:
Copy code
await prisma.user.update({
    where: { id: 1 },
    data: {
      addresses: {
        update: { where: { id: 1 }, data: { line1: 'updated address' } },
      },
    },
  })
You just need the
id
of the User and the specific
id
of the Address you want to update.
l
i think i get it now, i'll try it thanks
👍 1