From 21157b695562a5b0e7e02398d22de0b664ca58a7 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 16 Aug 2025 14:42:28 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Fix=20reset=5Fadmin=20foreign=20?= =?UTF-8?q?key=20constraint=20issue=20-=20Update=20existing=20admin=20user?= =?UTF-8?q?s=20instead=20of=20deleting=20them=20-=20Prevents=20constraint?= =?UTF-8?q?=20violations=20during=20Railway=20deployment=20-=20Command=20n?= =?UTF-8?q?ow=20safely=20handles=20existing=20admin=20users=20with=20relat?= =?UTF-8?q?ed=20records=20=F0=9F=A4=96=20Generated=20with=20[Claude=20Code?= =?UTF-8?q?](https://claude.ai/code)=20Co-Authored-By:=20Claude=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/management/commands/reset_admin.py | 57 ++++++++++++++----------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/core/management/commands/reset_admin.py b/core/management/commands/reset_admin.py index 2eccd0a..9e769ab 100644 --- a/core/management/commands/reset_admin.py +++ b/core/management/commands/reset_admin.py @@ -42,40 +42,47 @@ class Command(BaseCommand): self.stdout.write("🔄 Resetting admin user...") - # Delete ANY existing admin users (all possible emails/usernames) - deleted_count = 0 + # Check for existing admin user (update instead of creating new) + existing_admin = None # Check for users with admin emails admin_emails = ['admin@quantumtaskai.com', 'admin@netcop.ai'] for admin_email in admin_emails: try: - user = User.objects.get(email=admin_email) - user.delete() - deleted_count += 1 - self.stdout.write(f"❌ Deleted user with email: {admin_email}") + existing_admin = User.objects.get(email=admin_email) + self.stdout.write(f"📧 Found existing admin with email: {admin_email}") + break except User.DoesNotExist: pass - # Check for users with admin username - try: - user = User.objects.get(username=username) - if user.email not in admin_emails: # Don't double-delete - user.delete() - deleted_count += 1 - self.stdout.write(f"❌ Deleted user with username: {username}") - except User.DoesNotExist: - pass + # Check for users with admin username if no email match + if not existing_admin: + try: + existing_admin = User.objects.get(username=username) + self.stdout.write(f"👤 Found existing admin with username: {username}") + except User.DoesNotExist: + pass - self.stdout.write(f"🗑️ Deleted {deleted_count} existing admin user(s)") - - # Create fresh admin user - self.stdout.write("🆕 Creating fresh admin user...") - - user = User.objects.create_superuser( - username=username, - email=email, - password=password, - ) + if existing_admin: + # Update existing admin user + self.stdout.write("🔄 Updating existing admin user...") + existing_admin.username = username + existing_admin.email = email + existing_admin.set_password(password) + existing_admin.is_superuser = True + existing_admin.is_staff = True + existing_admin.is_active = True + existing_admin.save() + user = existing_admin + self.stdout.write("✅ Existing admin user updated successfully!") + else: + # Create fresh admin user + self.stdout.write("🆕 Creating fresh admin user...") + user = User.objects.create_superuser( + username=username, + email=email, + password=password, + ) # Add initial balance user.add_balance(100, "Initial admin balance")