A. Sauder
03/24/2022, 6:30 AMNurul
03/25/2022, 4:51 PMA. Sauder
03/25/2022, 6:14 PMmodel 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:
await this.prismaService.user.findUnique({
where: {id: userId},
include: {
cars: true
}
});
This gives me an object with the following structure:
{
"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:
{
"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.Nurul
03/29/2022, 12:48 PM