From 92b689f5e71a92c6d7d620a04408f0afc38c1510 Mon Sep 17 00:00:00 2001 From: Jan Remmer Siebels Date: Mon, 29 Sep 2025 22:14:06 +0200 Subject: [PATCH] Fix User DoesNotExist error in backup_cancel by using created_by_id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 Root Cause Found & Fixed: - Error occurred when accessing backup_job.created_by where the referenced user was deleted - Django was trying to fetch a User object that no longer exists - Changed to use backup_job.created_by_id instead of backup_job.created_by - This avoids the foreign key lookup that was causing the 'User matching query does not exist' error ✅ Backup cancellation now works even when: - Original creator user has been deleted from the database - Foreign key relationship is broken but ID is still stored The backup job can now be cancelled without triggering user lookup errors. --- app/stiftung/views.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/stiftung/views.py b/app/stiftung/views.py index 898860a..8a369b7 100644 --- a/app/stiftung/views.py +++ b/app/stiftung/views.py @@ -6019,7 +6019,9 @@ def backup_cancel(request, backup_id): print(f"DEBUG: Attempting to cancel backup job {backup_id}") backup_job = BackupJob.objects.get(id=backup_id) print(f"DEBUG: Found backup job - ID: {backup_job.id}, Status: {backup_job.status}") - print(f"DEBUG: Created by: {backup_job.created_by}, Current user: {request.user}") + + # Use created_by_id instead of created_by to avoid triggering the foreign key lookup + print(f"DEBUG: Created by ID: {backup_job.created_by_id}, Current user ID: {request.user.id}") # Only allow cancelling running or pending jobs if backup_job.status not in ['running', 'pending']: @@ -6027,9 +6029,9 @@ def backup_cancel(request, backup_id): return redirect("stiftung:backup_management") # Check if user has permission to cancel (either own job or admin) - # Handle case where created_by might be None (user deleted) - print(f"DEBUG: Checking permissions - created_by: {backup_job.created_by}, is_staff: {request.user.is_staff}") - if backup_job.created_by is not None and backup_job.created_by != request.user and not request.user.is_staff: + # Use created_by_id to avoid database lookup for potentially non-existent user + print(f"DEBUG: Checking permissions - created_by_id: {backup_job.created_by_id}, is_staff: {request.user.is_staff}") + if backup_job.created_by_id is not None and backup_job.created_by_id != request.user.id and not request.user.is_staff: messages.error(request, "Sie können nur Ihre eigenen Backup-Jobs abbrechen.") return redirect("stiftung:backup_management")