How to Execute Custom Logic When Deleting Related Rows
Hi there,
I’m working with Neon DB (serverless PostgreSQL) and have a scenario where deleting a product should cascade and delete related rows from other tables like variants, variant sizes, variant colors, and images.
PostgreSQL’s foreign key constraints handle the cascade deletion well, but I need to run additional logic when images are deleted. Specifically, I want to delete the image files from Cloudinary as part of the cleanup process.
3 Replies
rival-black•11mo ago
@M Zeeshan it sounds like you want to make an external HTTP API call upon delete of certain rows in the database.
This is perhaps a bit of personal preference, but I would recommend not putting the logic to delete the cloudinary images in your database, but rather in worker/task code in your application. This should provide better test/debug-ability.
What language/framework are you working in?
vicious-goldOP•11mo ago
Hi, thx for your precious time.
I am using nextjs and drizzle.
I have managed to delete cloudinary files and working very well. On delete, in hono.js route i fetch all variants and related images then delete them in drizzle transactions.
rival-black•11mo ago
I think that sounds like a much better approach than putting logic in the database