From 259ff992ae906442b32de8693219e5d4bfbc768e Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 15 Jul 2025 13:28:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=B4=A0=E6=9D=90?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=97=B6model=5Fid=E4=B8=BAnull=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在materials表初始创建时添加model_id列,避免依赖迁移 - 在初始索引创建中添加model_id索引 - 添加调试日志跟踪model_id在导入过程中的传递 - 确保素材导入时选择的模特ID能正确保存到数据库 - 解决素材导入+选择模特后数据库结果model_id=null的问题 --- .../src/business/services/material_service.rs | 3 ++ .../data/repositories/material_repository.rs | 5 ++- .../src-tauri/src/infrastructure/database.rs | 35 ++++++++++++++----- .../commands/material_commands.rs | 2 ++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/apps/desktop/src-tauri/src/business/services/material_service.rs b/apps/desktop/src-tauri/src/business/services/material_service.rs index eda5792..888f588 100644 --- a/apps/desktop/src-tauri/src/business/services/material_service.rs +++ b/apps/desktop/src-tauri/src/business/services/material_service.rs @@ -133,6 +133,9 @@ impl MaterialService { // 确定素材类型 let material_type = MaterialType::from_extension(extension); + // 添加调试日志 + println!("Creating material with model_id: {:?}", model_id); + // 创建素材对象(带模特绑定) let material = Material::new_with_model( project_id.to_string(), diff --git a/apps/desktop/src-tauri/src/data/repositories/material_repository.rs b/apps/desktop/src-tauri/src/data/repositories/material_repository.rs index 7646247..970f1a0 100644 --- a/apps/desktop/src-tauri/src/data/repositories/material_repository.rs +++ b/apps/desktop/src-tauri/src/data/repositories/material_repository.rs @@ -26,7 +26,10 @@ impl MaterialRepository { /// 创建素材 pub fn create(&self, material: &Material) -> Result<()> { let conn = self.connection.lock().unwrap(); - + + // 添加调试日志 + println!("Creating material with model_id: {:?}", material.model_id); + let metadata_json = serde_json::to_string(&material.metadata)?; let scene_detection_json = material.scene_detection.as_ref() .map(|sd| serde_json::to_string(sd)) diff --git a/apps/desktop/src-tauri/src/infrastructure/database.rs b/apps/desktop/src-tauri/src/infrastructure/database.rs index bbda94b..ce669f3 100644 --- a/apps/desktop/src-tauri/src/infrastructure/database.rs +++ b/apps/desktop/src-tauri/src/infrastructure/database.rs @@ -114,6 +114,7 @@ impl Database { "CREATE TABLE IF NOT EXISTS materials ( id TEXT PRIMARY KEY, project_id TEXT NOT NULL, + model_id TEXT, name TEXT NOT NULL, original_path TEXT NOT NULL, file_size INTEGER NOT NULL, @@ -402,6 +403,11 @@ impl Database { [], )?; + conn.execute( + "CREATE INDEX IF NOT EXISTS idx_materials_model_id ON materials (model_id)", + [], + )?; + // 创建模板表索引 conn.execute( "CREATE INDEX IF NOT EXISTS idx_templates_import_status ON templates (import_status)", @@ -875,18 +881,29 @@ impl Database { let has_model_id_column = conn.prepare("SELECT model_id FROM materials LIMIT 1").is_ok(); if !has_model_id_column { println!("Adding model_id column to materials table"); - conn.execute( + match conn.execute( "ALTER TABLE materials ADD COLUMN model_id TEXT", [], - )?; + ) { + Ok(_) => { + println!("Successfully added model_id column to materials table"); - // 创建模特关联索引 - conn.execute( - "CREATE INDEX IF NOT EXISTS idx_materials_model_id ON materials (model_id)", - [], - )?; - - println!("Added model_id column and index to materials table"); + // 创建模特关联索引 + match conn.execute( + "CREATE INDEX IF NOT EXISTS idx_materials_model_id ON materials (model_id)", + [], + ) { + Ok(_) => println!("Successfully created index on model_id column"), + Err(e) => println!("Warning: Failed to create index on model_id column: {}", e), + } + } + Err(e) => { + println!("Error adding model_id column to materials table: {}", e); + return Err(e.into()); + } + } + } else { + println!("model_id column already exists in materials table"); } // 添加片段匹配规则字段到轨道片段表 diff --git a/apps/desktop/src-tauri/src/presentation/commands/material_commands.rs b/apps/desktop/src-tauri/src/presentation/commands/material_commands.rs index 8af2c35..f47a4c5 100644 --- a/apps/desktop/src-tauri/src/presentation/commands/material_commands.rs +++ b/apps/desktop/src-tauri/src/presentation/commands/material_commands.rs @@ -42,6 +42,8 @@ pub async fn import_materials_async( request: CreateMaterialRequest, app_handle: tauri::AppHandle, ) -> Result { + // 添加调试日志 + println!("Import request received with model_id: {:?}", request.model_id); // 获取数据库连接,避免持有MutexGuard let connection = { let repository_guard = state.get_material_repository()