MichaelHindley
12/08/2020, 5:11 PMThe change you are trying to make would violate the required relation 'ModelAtoModelB' between the `ModelA` and `ModelB` models.
In the generated client, I can see that relationship has: “relationOnDelete”:“NONE”, but the table has on delete cascade as generated by the schema.prisma definitions. So how does one define this relationship so that when an owning side of an relationship is deleted, the children are deleted as well?Ryan
12/09/2020, 7:54 AMMichaelHindley
12/09/2020, 8:15 AMMichaelHindley
12/09/2020, 8:31 AMMichaelHindley
12/09/2020, 8:32 AMRyan
12/09/2020, 9:59 AMmodel User {
id Int @id @default(autoincrement())
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
published Boolean @default(false)
title String
content String?
authorId Int?
author User? @relation(fields: [authorId], references: [id])
}
I can define the 1-many relationship to be nullable by setting the authorId
as optional. This means that it’s an optional relationship. This can only be done where the actual scalar (authorId
) is defined and not the many side.Ryan
12/09/2020, 10:00 AMMichaelHindley
12/09/2020, 11:53 AMRyan
12/09/2020, 12:00 PMSupplyChain
then the SupplyChainTransportStep
connected to it will also be deleted if on delete cascade
has been set.
In my example, the User can be successfully deleted by id
and the corresponding posts will be deleted as well as I have specified on delete cascade
because the relationship is optional.MichaelHindley
12/10/2020, 9:47 AMALTER TABLE "SupplyChainTransportStep" ALTER COLUMN "supplyChainId" DROP NOT NULL
so it feels like on delete cascade
should also be added there from the SupplyChain
side?
Ihe SupplyChainTransportStep
table has this defined since before though:
"supplyChainId" text
constraint "SupplyChainTransportStep_supplyChainId_fkey"
references "SupplyChain"
on update cascade on delete cascade
Ryan
12/10/2020, 9:50 AMnot null
constraint and check after dropping it.
Also, if your SupplyChainTransportStep
has on delete cascade
then the following should work on optional relations.MichaelHindley
12/10/2020, 1:33 PMRyan
12/10/2020, 2:51 PMprisma.user.delete
as well.
Here’s the schema I tried on:
model User {
id Int @id @default(autoincrement())
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
user User @relation(fields: [userId], references: [id])
userId Int
}
At first, the relation is not null. I added random users with their posts.
When I try to delete the user, I get the error what you’re facing above.
Next I make the relation optional by adding a ?
and running migrate again:
model User {
id Int @id @default(autoincrement())
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
user User? @relation(fields: [userId], references: [id])
userId Int?
}
This time on running prisma.user.delete
it works perfectly fine.Ryan
12/10/2020, 2:52 PMRyan
12/11/2020, 7:04 AMMichaelHindley
12/14/2020, 1:20 PMMichaelHindley
12/22/2020, 5:04 PMprisma db push --preview-feature --force
and restoring from backup sql, that again let us delete things by id that have a cascade relationship (note that the schema did not change at all during all of this, it’s still the same as it was, but now without the client issue)Ryan
12/23/2020, 7:37 AMMichaelHindley
12/23/2020, 8:01 AMRyan
12/23/2020, 8:05 AMdb push
is actually meant to be used in development only as a prototyping mechanism 🙂Alberto Perdomo
MichaelHindley
12/23/2020, 9:51 AM