现在我有了一个数据库架构:
对于视频,它包含许多片段,对于片段,它包含许多作业,并且在每个作业中它包含许多路径等等...
现在我想复制片段并组合在一起制作新视频。我知道我可以编写一个脚本来从段到作业循环到...到bbox并逐个复制所有条目。但有没有更好的解决方案,我可以更聪明的方式进行深拷贝及其所有外来关系的输入?
答案 0 :(得分:0)
我试图为此找到一种解决方案,但最终按照一些用户在上面的评论链接中建议的那样添加了手动复制方法,但是我选择了一种更为手动的方法,以我为例,它简化了事情。以您的示例为例,我的解决方案将如下所示:
class Video(Model):
__tablename__ = 'videos'
id = Column(Integer, primary_key=True)
vidcol1 = Column(...)
vidcol2 = Column(...)
segments = relationship('Segment', uselist=True)
def copy(self):
new = Video()
new.vidcol1 = self.vidcol1
new.vidcol2 = self.vidcol2
for segment in self.segments:
new.segments.append(segment.copy())
return new
class Segment(Model):
__tablename__ = 'segments'
id = Column(Integer, primary_key=True)
video_id = Column(Integer, ForeignKey('videos.id'))
segcol1 = Column(...)
segcol2 = Column(...)
jobs = relationship('Job', uselist=True)
def copy(self):
new = Segment()
new.segcol1 = self.segcol1
new.segcol2 = self.segcol2
for job in self.jobs:
new.jobs.append(job.copy())
return new
class Job(Model):
__tablename__ = 'jobs'
id = Column(Integer, primary_key=True)
segment_id = Column(Integer, ForeignKey('segments.id'))
jobcol1 = Column(...)
jobcol2 = Column(...)
paths = relationship('Path', uselist=True)
def copy(self):
new = Job()
new.jobcol1 = self.jobcol1
new.jobcol2 = self.jobcol2
for path in self.paths:
new.paths.append(path.copy())
return new
class Path(Model):
__tablename__ = 'paths'
id = Column(Integer, primary_key=True)
job_id = Column(Integer, ForeignKey('jobs.id'))
pthcol1 = Column(...)
pthcol2 = Column(...)
bboxs = relationship('BBox', uselist=True)
def copy(self):
new = Path()
new.pthcol1 = self.pthcol1
new.pthcol2 = self.pthcol2
for bbox in self.bboxs:
new.bboxs.append(bbox.copy())
return new
class BBox(Model):
__tablename__ = 'bboxs'
id = Column(Integer, primary_key=True)
path_id = Column(Integer, ForeignKey('paths.id'))
boxcol1 = Column(...)
boxcol2 = Column(...)
def copy(self):
new = BBox()
new.boxcol1 = self.boxcol1
new.boxcol2 = self.boxcol2
return new
每个模型负责复制其自己的列并调用其直接关系的copy方法。这样,视频无需了解所有更深层次的关系,您可以执行以下操作:
video_copy = existing_video.copy()
session.add(video_copy)
session.commit()
在我的情况下,我也有多对多关系(作为辅助表和AssociationObjects)。如果您想添加其他类型的关系,那不会太复杂。