@@ -489,6 +489,19 @@ def migrate(self, with_valid=True):
489489 target_document_id , target_knowledge_id , None )
490490 # 修改段落信息
491491 paragraph_list .update (document_id = target_document_id )
492+
493+ # 将当前文档中所有段落的位置向下移动,为新段落腾出空间
494+ Paragraph .objects .filter (document_id = target_document_id ).exclude (
495+ id__in = paragraph_id_list
496+ ).update (position = F ('position' ) + len (paragraph_id_list ))
497+ # 重新查询迁移的段落
498+ paragraph_list = Paragraph .objects .filter (
499+ id__in = paragraph_id_list , document_id = target_document_id
500+ )
501+ # 将迁移的段落位置设置为从0开始的序号
502+ for i , paragraph in enumerate (paragraph_list ):
503+ paragraph .position = i
504+ paragraph .save ()
492505 # 不同数据集迁移
493506 else :
494507 problem_list = QuerySet (Problem ).filter (
@@ -522,6 +535,19 @@ def migrate(self, with_valid=True):
522535 pid_list = [paragraph .id for paragraph in paragraph_list ]
523536 # 修改段落信息
524537 paragraph_list .update (knowledge_id = target_knowledge_id , document_id = target_document_id )
538+
539+ # 将当前文档中所有段落的位置向下移动,为新段落腾出空间
540+ Paragraph .objects .filter (document_id = target_document_id ).exclude (
541+ id__in = pid_list
542+ ).update (position = F ('position' ) + len (pid_list ))
543+ # 重新查询迁移的段落
544+ paragraph_list = Paragraph .objects .filter (
545+ id__in = pid_list , document_id = target_document_id
546+ )
547+ # 将迁移的段落位置设置为从0开始的序号
548+ for i , paragraph in enumerate (paragraph_list ):
549+ paragraph .position = i
550+ paragraph .save ()
525551 # 修改向量段落信息
526552 update_embedding_document_id (pid_list , target_document_id , target_knowledge_id , embedding_model_id )
527553
0 commit comments