fix: 修复素材导入时model_id为null的数据库问题

- 在materials表初始创建时添加model_id列,避免依赖迁移
- 在初始索引创建中添加model_id索引
- 添加调试日志跟踪model_id在导入过程中的传递
- 确保素材导入时选择的模特ID能正确保存到数据库
- 解决素材导入+选择模特后数据库结果model_id=null的问题
This commit is contained in:
imeepos 2025-07-15 13:28:47 +08:00
parent 5686af3199
commit 259ff992ae
4 changed files with 35 additions and 10 deletions

View File

@ -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(),

View File

@ -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))

View File

@ -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");
}
// 添加片段匹配规则字段到轨道片段表

View File

@ -42,6 +42,8 @@ pub async fn import_materials_async(
request: CreateMaterialRequest,
app_handle: tauri::AppHandle,
) -> Result<MaterialImportResult, String> {
// 添加调试日志
println!("Import request received with model_id: {:?}", request.model_id);
// 获取数据库连接避免持有MutexGuard
let connection = {
let repository_guard = state.get_material_repository()