Home > Back-end >  Django - Heroku - debug error 500 when deleting an object in Prod
Django - Heroku - debug error 500 when deleting an object in Prod

Time:09-26

Problem:

  • Server Error 500 when trying to delete a user account in Prod.

Scenarios:

  • Delete a user account in Dev - works fine
  • Create a user account in Prod - works fine
  • Delete a user account in Prod - error 500

view.py:

@login_required 
def account_destroy_view(request, id=None, *args, **kwargs):   
  try:
    obj = request.user   
  except request.user.DoesNotExist:
    raise Http404   
  if request.method == "POST":
    try:
      customer = StripeRecord.objects.get(user_id=obj.id)
      stripe.api_key = config('STRIPE_API_KEY')
      stripe.Customer.delete(customer.stripe_customer_id)
    except StripeRecord.DoesNotExist:
      pass
    obj.delete()
    return redirect("/")
  return render(request, "accounts/delete.html", {"object": obj})

accounts model.py:

class User(AbstractUser):
  date_time = models.DateTimeField(default=datetime.datetime.now())

stripe model.py:

User = settings.AUTH_USER_MODEL

class StripeRecord(models.Model):
  user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
  ...

Heroku log:

Sep 23 19:37:37 weatherapp app/web.1 10.1.24.136 - - [24/Sep/2021:02:37:36  0000] "POST /delete/ HTTP/1.1" 500 145 "https://www.weatherapp.com/delete/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52"

What I've tried & questions:

  • I've checked Stripe logs, the object has been deleted on Stripe. So the issue isn't on the Stripe env var or integration.
  • Tried to delete a user account directly using Django admin tool in Prod - still error 500. I suspect it's because I created the model with AbstractUser and it is a foreignkey to another model?
  • How to get more details on the error? ( heroku logs --tail only returns what I've shown above )

CodePudding user response:

StripeRecord table missing primary key in Prod because I missed a migration after making changes on the model.

The problem was resolved after resetting the Prod DB (luckily it's a personal project).

  • Related