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.
This commit is contained in:
@@ -2642,6 +2642,7 @@ class Plugin:
|
||||
|
||||
channels_updated = 0
|
||||
total_streams_added = 0
|
||||
channels_skipped = 0
|
||||
|
||||
self._send_progress_update("add_streams_to_channels", 'running', 20,
|
||||
f'Processing {len(channel_groups)} channel groups...', context)
|
||||
@@ -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:
|
||||
@@ -2788,6 +2796,9 @@ class Plugin:
|
||||
|
||||
# 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}
|
||||
|
||||
Reference in New Issue
Block a user