gpt4 book ai didi

python - 如何从 SQLAlchemy 关系子表中删除所有行?

转载 作者:行者123 更新时间:2023-12-03 20:22:28 25 4
gpt4 key购买 nike

我在想这个任务是相当微不足道的,但我没有运气在谷歌上搜索它。如果我有一个 User 表,它与表 Videosupload_list 关系,你如何清除那个 upload_list 表?

我是这样构造的:

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())

最佳答案

是的,已阅读 this ,解决方案其实很明显。开始:s.query(models.UploadLists).filter(models.UploadLists.user_id == user.id).delete()

但是,上述模型似乎没有正确的级联删除配置。删除某些用户的 UploadList 仍然将其所有相关视频留在表 Videos 中.现在正在研究它,如果您知道答案,那么很高兴您将答案留在这里。如果答案正确,我会将其标记为已接受:)

关于python - 如何从 SQLAlchemy 关系子表中删除所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27085463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com