fix: resolve unique constraint username duplicate key violation by cleaning stale users on auth
Browse files153 unmodified lines154155156157158159160161162163164153 unmodified lines# 6. Find or Create the Usertry:user, created = User.objects.get_or_create(id=user_id,defaults={'email': email, 'username': email})if created:print(f"[AUTH DEBUG] ✅ Created new user: {email}")else:print(f"[AUTH DEBUG] ✅ Found existing user: {email}")153 unmodified lines154155156157158159160161162163164165166167168169170171172173174153 unmodified lines# 6. Find or Create the Usertry:# Check if user already exists with this UUIDuser = User.objects.filter(id=user_id).first()if not user:# Check if a user with the same email or username exists (stale user)stale_user = User.objects.filter(username=email).first() or User.objects.filter(email=email).first()if stale_user:print(f"[AUTH DEBUG] ⚠️ Stale user found with different UUID (existing: {stale_user.id}, new: {user_id}). Deleting stale user.")stale_user.delete()# Create the new user with the new UUIDuser = User.objects.create(id=user_id,email=email,username=email)print(f"[AUTH DEBUG] ✅ Created new user: {email}")else:print(f"[AUTH DEBUG] ✅ Found existing user: {email}")
users/authentication.py+15-5