fix:修复导入导出过程重复逻辑判断问题

This commit is contained in:
xiamuceer
2026-02-03 10:04:15 +08:00
parent de377a4117
commit 2ca179c9e7
+21 -23
View File
@@ -811,21 +811,16 @@ class ImportExportService:
logger.info(f"导入角色职业关联数: {char_careers_count}") logger.info(f"导入角色职业关联数: {char_careers_count}")
# 导入故事记忆 # 导入故事记忆
# 需要先构建章节标题到ID的映射 # 需要先构建章节标题到ID的映射(使用章节号+标题组合确保唯一性)
chapter_title_to_id = {} chapter_title_to_id = {}
for ch_data in data.get("chapters", []): chapter_result = await db.execute(
title = ch_data.get("title") select(Chapter).where(Chapter.project_id == new_project.id)
if title:
# 查询刚导入的章节
ch_result = await db.execute(
select(Chapter).where(
Chapter.project_id == new_project.id,
Chapter.title == title
) )
) imported_chapters = chapter_result.scalars().all()
ch = ch_result.scalar_one_or_none() for ch in imported_chapters:
if ch: # 使用标题作为key,如果有重复标题则取第一个(已导入的顺序)
chapter_title_to_id[title] = ch.id if ch.title and ch.title not in chapter_title_to_id:
chapter_title_to_id[ch.title] = ch.id
memories_count = await ImportExportService._import_story_memories( memories_count = await ImportExportService._import_story_memories(
new_project.id, data.get("story_memories", []), chapter_title_to_id, char_mapping, db new_project.id, data.get("story_memories", []), chapter_title_to_id, char_mapping, db
@@ -1107,7 +1102,8 @@ class ImportExportService:
WritingStyle.name == style_data.get("name") WritingStyle.name == style_data.get("name")
) )
) )
if existing.scalar_one_or_none(): # 使用 first() 避免多行时报错
if existing.first():
logger.debug(f"风格 {style_data.get('name')} 已存在,跳过导入") logger.debug(f"风格 {style_data.get('name')} 已存在,跳过导入")
continue continue
@@ -1172,14 +1168,14 @@ class ImportExportService:
career_id = career_mapping.get(career_name) career_id = career_mapping.get(career_name)
if char_id and career_id: if char_id and career_id:
# 检查是否已存在 # 检查是否已存在(使用 first() 避免多行时报错)
existing = await db.execute( existing = await db.execute(
select(CharacterCareer).where( select(CharacterCareer).where(
CharacterCareer.character_id == char_id, CharacterCareer.character_id == char_id,
CharacterCareer.career_id == career_id CharacterCareer.career_id == career_id
) )
) )
if existing.scalar_one_or_none(): if existing.first():
continue continue
char_career = CharacterCareer( char_career = CharacterCareer(
@@ -1275,11 +1271,11 @@ class ImportExportService:
if not chapter_id: if not chapter_id:
continue # 跳过找不到章节的分析 continue # 跳过找不到章节的分析
# 检查是否已存在该章节的分析 # 检查是否已存在该章节的分析(使用 first() 避免多行时报错)
existing = await db.execute( existing = await db.execute(
select(PlotAnalysis).where(PlotAnalysis.chapter_id == chapter_id) select(PlotAnalysis).where(PlotAnalysis.chapter_id == chapter_id)
) )
if existing.scalar_one_or_none(): if existing.first():
continue continue
analysis = PlotAnalysis( analysis = PlotAnalysis(
@@ -1355,14 +1351,15 @@ class ImportExportService:
return False return False
# 查找对应的风格(优先查找用户自定义风格,然后是全局预设风格) # 查找对应的风格(优先查找用户自定义风格,然后是全局预设风格)
# 先查用户自定义风格 # 先查用户自定义风格(使用 first() 避免多行时报错)
style_result = await db.execute( style_result = await db.execute(
select(WritingStyle).where( select(WritingStyle).where(
WritingStyle.user_id == project.user_id, WritingStyle.user_id == project.user_id,
WritingStyle.name == style_name WritingStyle.name == style_name
) )
) )
style = style_result.scalar_one_or_none() style_row = style_result.first()
style = style_row[0] if style_row else None
# 如果用户自定义风格不存在,查找全局预设风格 # 如果用户自定义风格不存在,查找全局预设风格
if not style: if not style:
@@ -1372,7 +1369,8 @@ class ImportExportService:
WritingStyle.name == style_name WritingStyle.name == style_name
) )
) )
style = style_result.scalar_one_or_none() style_row = style_result.first()
style = style_row[0] if style_row else None
if not style: if not style:
logger.warning(f"导入项目默认风格时未找到风格: {style_name}") logger.warning(f"导入项目默认风格时未找到风格: {style_name}")
@@ -1532,14 +1530,14 @@ class ImportExportService:
errors.append(f"{idx+1}个角色缺少name字段") errors.append(f"{idx+1}个角色缺少name字段")
continue continue
# 检查重复名称 # 检查重复名称(使用 first() 避免多行时报错)
existing_result = await db.execute( existing_result = await db.execute(
select(Character).where( select(Character).where(
Character.project_id == project_id, Character.project_id == project_id,
Character.name == name Character.name == name
) )
) )
existing = existing_result.scalar_one_or_none() existing = existing_result.first()
if existing: if existing:
warnings.append(f"角色'{name}'已存在,已跳过") warnings.append(f"角色'{name}'已存在,已跳过")