Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#15082 closed (invalid)

delete() does not function consistently with CharField PKs

Reported by: w004dal Owned by: nobody
Component: Core (Other) Version: 1.2
Severity: Keywords: model, db
Cc: w004dal@… Triage Stage: Unreviewed
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by russellm)

I'm working with MyISAM tables with MySQL and have the following behavior with delete() on Django 1.2.3-1 on Fedora Core 12.

class DoesWork(models.Model):
    mac_id = models.CharField(max_length=17)
    ip = models.CharField('Host/IP Address', max_length=255)

class DoesNotWork(models.Model):
    mac_id = models.CharField(max_length=17, primary_key=True)
    ip = models.CharField('Host/IP Address', max_length=255)

The following works as expected, creating, deleting, and putting the item back:

zz = DoesWork.objects.create(mac_id='99:99:99:99:99:99', ip='127.0.0.1')
zz.delete() # remove from DB
zz.save() # it's back in the DB

However, if I use the DoesNotWork model, whose only difference is having a CharField as a primary key:

zz = DoesNotWork.objects.create(mac_id='99:99:99:99:99:99', ip='127.0.0.1')
zz.delete() # remove from DB
zz.save() # EXCEPTION THROWN:

IntegrityError: (1048, "Column 'mac_id' cannot be null")

I checked by printing out zz.__dict__, and the mac_id was 'None' with the DoesNotWork object, but was untouched with the DoesWork object.

Attachments (0)

Change History (5)

comment:1 Changed 3 years ago by russellm

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Corrected formatting. Please use preview.

comment:2 Changed 3 years ago by russellm

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

This is working as expected. When you delete the object, it's primary key is set to None. In the model DoesNotWork, the primary key is manually allocated, so subsequent saves are prevented. In the model DoesWork, the primary key is automatically allocated, so a new object is inserted.

comment:3 Changed 3 years ago by w004dal

  • Needs documentation set

What is the expected behavior for the class below? DoesWork should be syntactic sugar for DoesNotWorkToo:

class DoesNotWorkToo(models.Model):
    some_key = models.IntegerField(primary_key=True)
    mac_id = models.CharField(max_length=17)
    ip = models.CharField('Host/IP Address', max_length=255)

comment:4 Changed 3 years ago by russellm

If you have questions, please ask them on django-users.

comment:5 Changed 3 years ago by w004dal

Please reopen this bug to at least improve the documentation on http://docs.djangoproject.com/en/dev/topics/db/queries to include this subtlety.

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.