0

Node.js (TypeScript) & PostgreSQL: onDelete: 'cascade' Not Working

Hi everyone,I'm building an API using Node.js with TypeScript and PostgreSQL. Each admin is associated with a user, and I want to delete the user when the admin is deleted. I've set onDelete: 'cascade' on the association, but it doesn't seem to work. All models are located in shared/src/db/models.Here is the link to my repository for reference: job-finder.Can someone help me understand why this isn't working? https://github.com/the-berufegoru/job-finder

30th Jul 2024, 3:17 PM
Kabelo Moobi
Kabelo Moobi - avatar
2 ответов
0
Hello there, From your post Description... "Each admin is associated with a user, and I want to delete the user when the admin is deleted." Looking at 'adminModel' and 'recruiterModel' at a glance, I see both these references 'userModel'. Please correct me if I misunderstood it. As I understand it (from SQL POV), setting CASCADE option for DELETE event in SQL means - when a record from the referenced table (user) is deleted, any matching record in referencing tables wherein foreign key was defined (admin, recruiter) will be deleted as well. This apparently does not go the other way around. https://dev.to/bhanufyi/understanding-on-delete-cascade-and-on-update-cascade-in-sql-foreign-key-relationships-70o https://stackoverflow.com/a/7291823 And by that, I guess your idea of deleting a 'user' by deleting an 'admin' (which references 'user') is perhaps a no go. As the CASCADE option for DELETE event is triggered on deletion of record in referenced table (user) not the referencing table (admin, recruiter).
30th Jul 2024, 8:17 PM
Ipang
0
What matters is the relationship, CASCADE works for a one-to-one relationship
31st Jul 2024, 12:08 AM
Mel