python - One to Many doesn't remove foreign key on delete in SQLAlchemy - Stack Overflow

Here's the setup:A User can have many images assigned (related_images), but can only have 1 profi

Here's the setup:

A User can have many images assigned (related_images), but can only have 1 profile image.

class Photo(Model):
    __tablename__ = "photo"

    id = Column(Integer, primary_key=True, autoincrement=True)
    filepath = Column(Text)

    user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"))
    user = db.relationship("User", foreign_keys=[user_id], back_populates="related_images", lazy=True)

class User(Model):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Text)
    
    related_images = db.relationship("Photo", foreign_keys=[Photo.user_id], back_populates="user", lazy=True, cascade="delete")

    profile_image_id = Column(Integer, ForeignKey("photo.id", ondelete="CASCADE"))
    profile_image = db.relationship("Photo", foreign_keys=[profile_image_id], lazy=True, post_update=True, cascade="delete")

I want to delete a User's profile image, so I do:

user = get_user(user_id)
db.session.delete(user.profile_image)
db.sessionmit()

This will successfully delete the row from the Photo table, however user.profile_image_id on the User table will still hold the old photo id, id 1 in this case.

How do I delete the user's profile photo so that it's removed (or no longer referenced) from the User table too? The ondelete and cascade='delete' don't seem to work here.

Here's the setup:

A User can have many images assigned (related_images), but can only have 1 profile image.

class Photo(Model):
    __tablename__ = "photo"

    id = Column(Integer, primary_key=True, autoincrement=True)
    filepath = Column(Text)

    user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"))
    user = db.relationship("User", foreign_keys=[user_id], back_populates="related_images", lazy=True)

class User(Model):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Text)
    
    related_images = db.relationship("Photo", foreign_keys=[Photo.user_id], back_populates="user", lazy=True, cascade="delete")

    profile_image_id = Column(Integer, ForeignKey("photo.id", ondelete="CASCADE"))
    profile_image = db.relationship("Photo", foreign_keys=[profile_image_id], lazy=True, post_update=True, cascade="delete")

I want to delete a User's profile image, so I do:

user = get_user(user_id)
db.session.delete(user.profile_image)
db.sessionmit()

This will successfully delete the row from the Photo table, however user.profile_image_id on the User table will still hold the old photo id, id 1 in this case.

How do I delete the user's profile photo so that it's removed (or no longer referenced) from the User table too? The ondelete and cascade='delete' don't seem to work here.

Share Improve this question edited Nov 17, 2024 at 17:35 doejoe asked Nov 17, 2024 at 6:22 doejoedoejoe 1653 silver badges10 bronze badges 2
  • The title specifies a one-to-many relationship, but each table has an FK to the other. Please edit the question to clarify how you expect the relationship to behave. – snakecharmerb Commented Nov 17, 2024 at 14:59
  • Hi, I edited my question. Basically a user has related images, but only a single profile image. In order to reuse the Photo model, I need to define an FK on the User model, so that's why there are two FKs. However, the issue is when deleting the profile photo only, the FK on the User is not cleared. If this isn't the way to set up the Models, it'll be helpful for me to see what the proper model setup should be. – doejoe Commented Nov 17, 2024 at 17:37
Add a comment  | 

1 Answer 1

Reset to default 0

class Photo(Model): tablename = "photo"

id = Column(Integer, primary_key=True, autoincrement=True)
filepath = Column(Text)

user_id = Column(Integer, ForeignKey("user.id", ondelete="CASCADE"))
user = db.relationship("User", foreign_keys=[user_id], back_populates="related_images", lazy='select')

class User(Model): tablename = "user"

id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(Text)

related_images = db.relationship("Photo", foreign_keys=[Photo.user_id], back_populates="user", lazy='select', cascade="all, delete-orphan")

profile_image_id = Column(Integer, ForeignKey("photo.id", ondelete="SET NULL"), nullable=True)
profile_image = db.relationship("Photo", foreign_keys=[profile_image_id], lazy='joined', post_update=True, cascade="all, delete-orphan")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636980a4637456.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信