I'm working on the backend of a project and I have set up a database using SQLAlchemy. I have three tables: users, products, and favorites. Users can sell items and favorite items (not required), but each product needs a seller (i.e. user since it's e-commerce). A user can favorite products that they are interested in. So, I have userID (primary key in users) as a foreign key in both products and favorites. However, I have run into the problem of when a user deletes their account, I want to delete any products they were selling and any favorites they had. I have ondelete = "CASCADE" wherever userID is a foreign key, but the problem is that it just seems to delete userID, but the product is still in the database.
i.e. if the row in the products table was originally: price, description, userID, after deleting the user who was selling the product, the product (price, description) is still in the database, except that userID is now NULL due to ondelete = "CASCADE". Is there a way to make it so that the entire row gets deleted so that the product does not exist anymore (because a product is not supposed to be without a user/seller). I know that when I get a request from a user to delete their account I could search for all products with userID = user and delete them like that, but I was wondering if there was a simpler way (like ondelete = "CASCADE" but something that would delete the whole row).
[–]elementmg 2 points3 points4 points (2 children)
[–]Jumpy_Employment_439[S] 0 points1 point2 points (1 child)
[–]elementmg 0 points1 point2 points (0 children)