2014-11-23 65 views
1

我在想這個任務是相當微不足道的,但我沒有運氣在Google上搜索它。如果我有一個User表,那就有upload_list關係成表Videos,你怎麼清除upload_list表呢?如何從SQLAlchemy關係子表中刪除所有行?

我構建這樣說:

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

放下桌子並重新創建它? – IanAuld 2014-11-23 04:38:32

+0

..但是其他用戶的所有其他'upload_lists'數據都將被刪除。 – swdev 2014-11-23 09:15:10

回答

0

呀,看了this,該解決方案是非常明顯的實際。這裏所說: s.query(models.UploadLists).filter(models.UploadLists.user_id == user.id).delete()

但儘管如此,上述模型似乎沒有一個正確的級聯刪除configuration.Deleting的UploadList對於某些用戶仍然留下所有的留在表Videos其相關視頻。現在就開始工作吧,如果你知道答案,那麼你在這裏留下答案真是太好了。如果它是正確答案,我會將其標記爲已接受:)

相關問題