From 937ccec94ea620e2374943ace90257337b6a3311 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 28 Nov 2025 17:04:36 +0000 Subject: [PATCH] Fix: Add channel existence validation to prevent foreign key constraint violations Addresses issue where stale cached channel data causes foreign key errors when attempting to create ChannelStream relationships for channels that no longer exist in the database. Changes: - Add pre-validation check using Channel.objects.filter(id).exists() before attempting any ChannelStream operations - Gracefully skip deleted channels with warning log message including channel name and ID - Track count of skipped channels and report in final success message - Suggest reloading channels when skipped channels are detected This prevents the error: "insert or update on table dispatcharr_channels_channelstream violates foreign key constraint" when channel_id is not present in the channels table. --- Stream-Mapparr/plugin.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Stream-Mapparr/plugin.py b/Stream-Mapparr/plugin.py index 76523b6..f472d2c 100644 --- a/Stream-Mapparr/plugin.py +++ b/Stream-Mapparr/plugin.py @@ -2642,10 +2642,11 @@ class Plugin: channels_updated = 0 total_streams_added = 0 - - self._send_progress_update("add_streams_to_channels", 'running', 20, + channels_skipped = 0 + + self._send_progress_update("add_streams_to_channels", 'running', 20, f'Processing {len(channel_groups)} channel groups...', context) - + processed_groups = 0 total_groups = len(channel_groups) @@ -2660,6 +2661,13 @@ class Plugin: for channel in channels_to_update: channel_id = channel['id'] + + # Validate that channel exists in database before attempting operations + if not Channel.objects.filter(id=channel_id).exists(): + logger.warning(f"[Stream-Mapparr] Skipping channel '{channel['name']}' (ID: {channel_id}) - channel no longer exists in database. Consider reloading channels.") + channels_skipped += 1 + continue + try: if matched_streams: if overwrite_streams: @@ -2785,11 +2793,14 @@ class Plugin: logger.error(f"[Stream-Mapparr] Failed to create scheduled CSV export: {e}") self._trigger_frontend_refresh(settings, logger) - + # Send final completion notification success_msg = f"Updated {channels_updated} channels with {total_streams_added} streams." + if channels_skipped > 0: + success_msg += f" Skipped {channels_skipped} deleted channel(s)." + logger.info(f"[Stream-Mapparr] {success_msg}") self._send_progress_update("add_streams_to_channels", 'success', 100, success_msg, context) - + return {"status": "success", "message": success_msg} except Exception as e: