Code

Opened 5 months ago

Closed 5 months 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

Description

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'):
        location.delete()
        messages.success(request, 'Location {0} was deleted.'.format(location_id))
        logger.info('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' location.id %}

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.

Attachments (0)

Change History (3)

comment:1 Changed 5 months ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

https://github.com/django-mptt/django-mptt/issues/151

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 follow-up: Changed 5 months 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 5 months ago by claudep

  • Resolution set to needsinfo
  • Status changed from new to closed

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?

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.