Hello together I don't know if I just didn't under...
# orm-help
a
Hello together I don't know if I just didn't understand something basic, or if prisma just doesn't offer it that way. Namely, I have for example a user who can have multiple cars via a 1:n. If I now read the user and set the connection to the cars to true via includes, I get the user which has all cars in the field "cars". If I now add another auto in this field in the GUI, and send the user object back to the backend, I currently take apart the user object to store the pure user, and then the cars. Is there no way to tell prisma directly "hey, here I have the user's cars, see which ones need to be deleted, which ones get an update, and which ones need to be recreated"? Currently I get the autos that exist now, and make different filters on them. This results in the cars to be deleted, the ones that are new and the ones that get an update. Here I would be very happy for tips.
n
Hey 👋 I cannot understand what you are trying to achieve, can you share your schema and the query which you are invoking? I could have a look and see if there is a better way to do it
a
Hey Here is the scheme:
Copy code
model User {
  id                String          @default(uuid()) @id
  email             String          @unique
  name              String?
  cars              Car[]
}

model Car {
    id              String         @default(uuid()) @id
    type            String
    year            Int
    userId          String
    user            User           @relation(fields: [userId], references: [id])
}
I run the query like this:
Copy code
await this.prismaService.user.findUnique({
      where: {id: userId},
      include: {
        cars: true
      }
    });
This gives me an object with the following structure:
Copy code
{
  "id": "569e0bed-a951-41b4-8c22-795b0bc46441",
  "email": "<mailto:test@test.ch|test@test.ch>",
  "name": "Testi Tester",
  "cars": [
    {
      "id": "891061ef-0527-483f-9932-6ca196eee963",
      "type": "Audi",
      "year": 2020,
      "userId": "569e0bed-a951-41b4-8c22-795b0bc46441"
    },
    {
      "id": "b70b3c71-128b-4b36-bb02-a57f41b075d7",
      "type": "BMW",
      "year": 2016,
      "userId": "569e0bed-a951-41b4-8c22-795b0bc46441"
    }
  ]
}
Now I pass it out to the frontend like this. From the frontend I get the following object back:
Copy code
{
  "id": "569e0bed-a951-41b4-8c22-795b0bc46441",
  "email": "<mailto:test@test.ch|test@test.ch>",
  "name": "Testi Tester",
  "cars": [
    {
      "id": "891061ef-0527-483f-9932-6ca196eee963",
      "type": "Audi",
      "year": 2020,
      "userId": "569e0bed-a951-41b4-8c22-795b0bc46441"
    },
    {
      "id": "b70b3c71-128b-4b36-bb02-a57f41b075d7",
      "type": "BMW",
      "year": 2016,
      "userId": "569e0bed-a951-41b4-8c22-795b0bc46441"
    },
    {
      "id": "8c035fa3-8fcb-424d-82df-312d092d28c3",
      "type": "Chevrolet Camaro",
      "year": 1969,
      "userId": "569e0bed-a951-41b4-8c22-795b0bc46441"
    }
  ]
}
Until now I have not found a way in Prisma to convert this object into a
this.prismaService.user.update({where: {id: '569e0bed-a951-41b4-8c22-795b0bc46441'}, data: updateRequest});
and prisma notices that it has a new car and inserts it. I always had to split the object and handle the cars separately until now. Here I hope for your help. Maybe you can show me how you solve this with you and that gives me the possibility to handle it easier with me.
n
Thank you for describing this in detail, if the object that you are getting from frontend is the source of truth, you are probably looking for set operator. Can you have a look at it?