Hey! Sorry for bothering you guys, but i was hopin...
# orm-help
d
Hey! Sorry for bothering you guys, but i was hoping to find some help here. I am using PostgreSQL in combination with Prisma2 and i am trying to cascade delete. What i found on google was that i need to cascade on database level - unlike in prisma1. My Table looks as follows
Copy code
CREATE TABLE presets (
    id SERIAL PRIMARY KEY,
    uid character(5) NOT NULL REFERENCES tokens(uid) ON DELETE CASCADE,
    deviceid integer NOT NULL,
    data jsonb NOT NULL,
    synced smallint NOT NULL DEFAULT 0,
    isdeleted boolean NOT NULL DEFAULT false,
    lfid integer,
    error jsonb
);
Now when i am trying to delete a row on said
tokens
table
Copy code
await prisma.tokens.delete({
	where: {
		uid
	}
});
it gives me an violation error
Copy code
Invalid `prisma.tokens.delete()` invocation in
path/dist/data.js:314:84


  The change you are trying to make would violate the required relation 'tokens' between the `presets` and `tokens` models.
    at PrismaClientFetcher.request (path/node_modules/@prisma/client/runtime/index.js:1:185690)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  code: 'P2014',
  meta:
   { relation_name: 'tokens',
     model_a_name: 'presets',
     model_b_name: 'tokens' } }
I am quite confused on what i am doing wrong, any tips appreciated.
d
I haven’t done this so I can’t help you directly, Daniel, but have you checked the documentation on setting up cascading deletes? I was reading it yesterday so had it in mind. https://www.prisma.io/docs/guides/database-workflows/cascading-deletes/postgresql If you still have problems I’m sure one of the many helpful people on here can assist. Good luck!
r
Hey @Daniel 👋 This is due to the issue referenced here. As a workaround, you would need to make your
uid
foreign key field nullable. i.e. Remove the
not null
constraint.
💯 2
d
Thank you Ryan!
💯 1