3

I was thinking that this task is rather trivial, but I have no luck in googling about it. If I have a User table, that has upload_list relationships into table Videos, how do you clear that upload_list table?

I construct it this way:

class Users(base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    email = Column(String(100), unique=True)
    password = Column(String(255))
    fullname = Column(String(255))
    photo = Column(String(255))
    last_updated = Column(DateTime())

    upload_lists = relationship('UploadLists', cascade="all, delete, delete-orphan")


class UploadLists(base):
    __tablename__ = 'upload_lists'
    id = Column(Integer, primary_key=True)
    list_id = Column(String(255), unique=True)
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship("Users")

    videos = relationship('Videos', cascade="all, delete, delete-orphan")


class Videos(base):
    __tablename__ = 'videos'
    id = Column(Integer, primary_key=True)
    video_id = Column(String(255), unique=True)
    title = Column(String(255))
    description = Column(String(255))
    upload_date = Column(DateTime)
    thumbnail_url = Column(Text())
    thumbnail_file = Column(Text())
swdev
  • 4,997
  • 8
  • 64
  • 106

1 Answers1

0

Yeah, having read this, the solution is pretty obvious actually. Here goes: s.query(models.UploadLists).filter(models.UploadLists.user_id == user.id).delete()

But still, the above models seems to not having a proper cascading delete configuration.Deleting the UploadList for certain User still left all of its related videos left in table Videos. Working on it now, and if you know the answer, it's great that you left the answer here. I'll mark it as accepted if it's a correct answer :)

Community
  • 1
  • 1
swdev
  • 4,997
  • 8
  • 64
  • 106