修复穿搭图片功能数据库表缺失和统计查询NULL值问题

- 在app_state.rs中添加OutfitImageRepository.init_tables()调用,确保穿搭图片相关表在应用启动时正确创建
- 修复get_stats_by_model_id方法中的SQL查询,使用COALESCE函数处理SUM聚合函数在空表时返回NULL的问题
- 解决'no such table: outfit_image_records'和'Invalid column type Null'错误
- 确保穿搭图片统计功能在没有数据时也能正常返回0值而不是NULL
This commit is contained in:
imeepos 2025-07-30 15:44:23 +08:00
parent 44ef1959cc
commit 3cd79a2f5d
2 changed files with 13 additions and 5 deletions

View File

@ -75,6 +75,10 @@ impl AppState {
video_generation_repository.init_tables()?; video_generation_repository.init_tables()?;
conversation_repository.initialize_tables()?; conversation_repository.initialize_tables()?;
// 初始化穿搭图片相关表
let outfit_image_repository = crate::data::repositories::outfit_image_repository::OutfitImageRepository::new(database.clone());
outfit_image_repository.init_tables()?;
*self.database.lock().unwrap() = Some(database.clone()); *self.database.lock().unwrap() = Some(database.clone());
*self.project_repository.lock().unwrap() = Some(project_repository); *self.project_repository.lock().unwrap() = Some(project_repository);
*self.material_repository.lock().unwrap() = Some(material_repository); *self.material_repository.lock().unwrap() = Some(material_repository);
@ -104,6 +108,10 @@ impl AppState {
video_generation_repository.init_tables()?; video_generation_repository.init_tables()?;
conversation_repository.initialize_tables()?; conversation_repository.initialize_tables()?;
// 初始化穿搭图片相关表
let outfit_image_repository = crate::data::repositories::outfit_image_repository::OutfitImageRepository::new(database.clone());
outfit_image_repository.init_tables()?;
*self.database.lock().unwrap() = Some(database.clone()); *self.database.lock().unwrap() = Some(database.clone());
*self.project_repository.lock().unwrap() = Some(project_repository); *self.project_repository.lock().unwrap() = Some(project_repository);
*self.material_repository.lock().unwrap() = Some(material_repository); *self.material_repository.lock().unwrap() = Some(material_repository);

View File

@ -371,10 +371,10 @@ impl OutfitImageRepository {
r#" r#"
SELECT SELECT
COUNT(*) as total_records, COUNT(*) as total_records,
SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_records, COALESCE(SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END), 0) as pending_records,
SUM(CASE WHEN status = 'processing' THEN 1 ELSE 0 END) as processing_records, COALESCE(SUM(CASE WHEN status = 'processing' THEN 1 ELSE 0 END), 0) as processing_records,
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as completed_records, COALESCE(SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END), 0) as completed_records,
SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed_records COALESCE(SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END), 0) as failed_records
FROM outfit_image_records WHERE model_id = ?1 FROM outfit_image_records WHERE model_id = ?1
"#, "#,
).map_err(|e| anyhow!("准备查询语句失败: {}", e))?; ).map_err(|e| anyhow!("准备查询语句失败: {}", e))?;
@ -395,7 +395,7 @@ impl OutfitImageRepository {
r#" r#"
SELECT SELECT
COUNT(*) as total_images, COUNT(*) as total_images,
SUM(CASE WHEN is_favorite = 1 THEN 1 ELSE 0 END) as favorite_images COALESCE(SUM(CASE WHEN is_favorite = 1 THEN 1 ELSE 0 END), 0) as favorite_images
FROM outfit_images oi FROM outfit_images oi
JOIN outfit_image_records oir ON oi.outfit_record_id = oir.id JOIN outfit_image_records oir ON oi.outfit_record_id = oir.id
WHERE oir.model_id = ?1 WHERE oir.model_id = ?1