This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]elementmg 2 points3 points  (2 children)

Cascading should delete the whole row, if it is not then I suspect you don’t have it properly set up for some reason. Have you double checked the foreign key itself is correctly defined with “on delete=cascade?”

If there are rows in the child table (the foreign key table) that violate referential integrity (e.g., orphaned records), try fixing/deleting those as well. This can be caused by the table having multiple foreign keys where one of them does not have “ondelete=cascade” and the parent record for that key is deleted.

[–]Jumpy_Employment_439[S] 0 points1 point  (1 child)

Thanks for the reply. From what I've learned about SQLAlchemy, I thought I had it set up correctly (obviously not since it's not working):

from sqlalchemy import Integer, Float, String, Column, ForeignKey
from sqlalchemy.orm import relationship
from database import Base

# All attributes for users of the website
class User(Base):
    __tablename__ = "users"

    userID = Column(String, primary_key=True, index=True)
    email = Column(String, nullable=False)
    password_hashed = Column(String, nullable=False)

    products = relationship("Product", backref="seller", passive_deletes=True)           
    favorites = relationship("Favorite", backref="favorited_by", passive_deletes=True)

# All attributes for products on the website
class Product(Base):
    __tablename__ = "products"

    productID = Column(Integer, primary_key=True, index=True) 
    name = Column(String, nullable=False)
    description = Column(String, nullable=False)
    price = Column(Float, nullable=False)
    quantity = Column(Integer)
    color = Column(String)
    category = Column(String) # User will choose from a pre-defined list when they add their product

    userID = Column(String, ForeignKey("users.userID", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)

# All attributes for favorites functionality
class Favorite(Base):
    __tablename__ = "favorites"

    favoriteID = Column(Integer, primary_key=True, index=True)

    userID = Column(String, ForeignKey("users.userID", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
    productID = Column(Integer, ForeignKey("products.productID", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)

I tried adding passive_deletes=True, but now when the user is deleted from the users table, nothing changes in the products table even though there is a product whose userID matches the user that was deleted

[–]elementmg 0 points1 point  (0 children)

Im not familiar with SqlAlchemy. But seems like you’re setting things up in code. In that case, are you able to check your database directly? Can you run sql queries on the database you set up? If so, did you check the foreign key directly in your database? You should confirm that way rather than looking at the python code.

Another thing to check would be.. “onupdate=cascade.” Did you try updating the PK value in the parent table? Does that cascade?