Opened 4 years ago

Closed 4 years ago

#22089 closed Uncategorized (needsinfo)

Deleting object with custom primary_key causes the pk to be None

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I am using PostgreSQL 9.3. I can reproduce this error every time. I have the following object model for my application:

class Location(models.Model):
    id = models.CharField(primary_key=True, max_length=20)
    ..  bunch of other non required fields ...

The location details view looks like this :

def location(request, location_id):
    # get the requested location
    location = Location.objects.get(pk=location_id)


   elif request.POST.get('delete-button'):
        messages.success(request, 'Location {0} was deleted.'.format(location_id))'User {0} has deleted location {1}.'.format(request.user, location_id))
        return redirect(reverse('locations_home'))

The redirect goes to a main locations page that runs a query set to get all locations

ls = Location.objects.all()

For each location a link is built using {% url 'locaion_page' %}

The deleted object seems to be still pulled into the queryset as the delete operation didn't complete but the id field is now None or a blank character in the view. The application crashes with a NoReverse error for location_page with u

Not sure why this started happening, it wasn't ever a problem, but now deletes seem to take longer and the amount of locations hasn't increased. ~ 100.

When i did location.delete() in the shell and quickly ran location again, i noticed it was still there. I ran location.delete() again and I got an AssertionError which made sense.

I suspect this is a bug related to using a CharField primary key.

Change History (3)

comment:1 Changed 4 years ago by anonymous

I also found this error here, and they concluded that switching to the default id fixes it all:

This is actually one of the things that was done by the previous programmer, he decided to make a CharField primary key. Bad idea .. but it identifies the location # uniquely using an integer. I will see if I can find a workaround. If having a CharField primary key causes the object to be partially deleted if a queryset to get all objects is ran right after then maybe CharFields should not be allowed to be pk.

I'm going to do more investigating in the meantime...

comment:2 Changed 4 years ago by anonymous

I didn't encounter the issue after adding blank=False to the model field & required no schema changes:

class Location(models.Model):

id = models.CharField(primary_key=True, max_length=20, blank=False)

comment:3 in reply to:  2 Changed 4 years ago by Claude Paroz

Resolution: needsinfo
Status: newclosed

Replying to anonymous:

I didn't encounter the issue after adding blank=False to the model field & required no schema changes:

That's weird, because blank=False is the default value anyway...

Could you maybe provide us a minimal project to be able to reproduce the issue?

Note: See TracTickets for help on using tickets.
Back to Top