diff --git a/src-tauri/src/repository/offline.rs b/src-tauri/src/repository/offline.rs index 7872333..381701d 100644 --- a/src-tauri/src/repository/offline.rs +++ b/src-tauri/src/repository/offline.rs @@ -162,6 +162,26 @@ impl OfflineRepository { let now = chrono::Utc::now().to_rfc3339(); + // Temporarily disable foreign key constraints to avoid CASCADE DELETE issues + // when replacing stub parent items with their actual data + self.db_service.execute(Query::new("PRAGMA foreign_keys = OFF")).await + .map_err(|e| RepoError::Database { message: e })?; + + // Ensure we re-enable foreign keys even if an error occurs + let result = self.save_to_cache_impl(parent_id, items, &now).await; + + // Re-enable foreign key constraints + let _ = self.db_service.execute(Query::new("PRAGMA foreign_keys = ON")).await; + + result + } + + async fn save_to_cache_impl( + &self, + parent_id: &str, + items: &[MediaItem], + now: &str, + ) -> Result { // Collect all unique parent IDs referenced by items being saved let mut parent_ids = std::collections::HashSet::new(); parent_ids.insert(parent_id.to_string()); @@ -185,7 +205,7 @@ impl OfflineRepository { QueryParam::String(self.server_id.clone()), QueryParam::String("Parent".to_string()), QueryParam::String("Folder".to_string()), - QueryParam::String(now.clone()), + QueryParam::String(now.to_string()), ], ); @@ -206,8 +226,6 @@ impl OfflineRepository { let backdrop_tags_json = item.backdrop_image_tags.as_ref() .map(|b| serde_json::to_string(b).unwrap_or_else(|_| "[]".to_string())); - let now = chrono::Utc::now().to_rfc3339(); - // Use INSERT OR REPLACE to upsert items let query = Query::with_params( "INSERT OR REPLACE INTO items ( @@ -315,7 +333,7 @@ impl OfflineRepository { Some(r) => QueryParam::String(r.clone()), None => QueryParam::Null, }, - QueryParam::String(now.clone()), + QueryParam::String(now.to_string()), ], );