This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
Cascading deletes #328
Labels
area/introspection
area/migrate
area/schema
keep
kind/discussion
Discussion about if and how something should be written down in a spec
Currently, we support cascading deletes with
@relation(onDelete:CASCADE)
. This is working today across introspection and lift.Cascading deletes in Prisma work differently than SQL. In Prisma, the way cascading deletes work is if I get deleted, delete my children.
In SQL, the way cascading deletes work is if my parent gets deleted, delete me.
Problems
@relation(onDelete:CASCADE)
on either side of the relationship, but if you run lift up, then introspect, you'll end up with@relation(onDelete:CASCADE)
only on the back-relation (e.g.Post[]
).Post[]
) not the forward-relation (e.g.Blog
).Proposal
Cascading deletes only matter with Introspection, Lift and the Schema. Since we only care about Introspection & Photon for GA, we only need to define the syntax for cascading deletes.
For this, I propose 2 adjustments:
1. Cascading deletes are always only present on the forward relation.
These rules will make sure that when we run
lift up
, thenprisma introspect
, we'll end up with the same schema.Rule A) We store on delete cascade where the data about the relation is stored.
This is the same as how SQL works. What's intuitive for some is unintuitive for others. We're not trying to fix SQL with PSL.
Rule B) Storing on the back-relation is a compiler error.
We can make this less painful with
prisma format
.Example Changes
I've added some examples of how we should change from our current approach to this proposal's approach.
One-to-One:
Only store on the side where the relation is stored, never on the back-relation.
Resulting SQL:
One-to-Many:
Resulting SQL:
Many-to-Many (explicit):
Resulting SQL:
Composite Keys:
Resulting SQL:
2. We change
@relation(onDelete:CASCADE)
toonDelete(cascade)
.This is just a matter of preference but not a blocker or anything.
The text was updated successfully, but these errors were encountered: