-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cascade delete does not work unless relation is optional #2212
Comments
I can confirm this. We need to loosen the validation in the query engine |
SQL of the database for direct reproduction: DROP TABLE IF EXISTS "public"."Post";
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS "Post_id_seq";
-- Table Definition
CREATE TABLE "public"."Post" (
"authorId" int4 NOT NULL,
"id" int4 NOT NULL DEFAULT nextval('"Post_id_seq"'::regclass),
PRIMARY KEY ("id")
);
DROP TABLE IF EXISTS "public"."User";
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS "User_id_seq";
-- Table Definition
CREATE TABLE "public"."User" (
"id" int4 NOT NULL DEFAULT nextval('"User_id_seq"'::regclass),
PRIMARY KEY ("id")
);
INSERT INTO "public"."Post" ("authorId", "id") VALUES
('1', '1');
INSERT INTO "public"."User" ("id") VALUES
('1');
ALTER TABLE "public"."Post" ADD FOREIGN KEY ("authorId") REFERENCES "public"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
You can introspect that database and try a deletion query as shown below to reproduce this: const del = await prisma.user.delete({
where: {
id: 1,
},
}); |
This is not a bug but by design. We enforce required relations on the Prisma layer. The schema clearly states that
|
Please don't say it's not a bug. Changing all relations to optional would generate TS types as nullable and that means a lot of (previously unnecessary) null checking in TS. Hopefully this can be fixed shortly, as it's quite annoying. Thanks for great work on prisma! |
duplicate of: #2057 |
Closing as a duplicate of #2057 |
Bug description
When I set db CONSTRAINT to CASCADE on delete and relation is marked as required in schema, prisma errors with
It works when I set the foreign key and the relation field as optional in prisma schema.
How to reproduce
Expected behavior
The user and all his posts should be deleted.
Prisma information
schema:
Environment & setup
DB: Postgres 11.4
Prisma: 2.0.0-beta.2
Node: 12.2.0
The text was updated successfully, but these errors were encountered: