Opened 8 years ago

Closed 7 years ago

#3774 closed (fixed)

Custom primary key allows to record a null or blank value

Reported by: revil Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: primary_key, custom primary key
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In a model I've used the following syntax for a custom primary key field:

name=models.SlugField('Name',maxlength=12,primary_key=True,

help_text='Descriptive name for the object')

But when I've entered in the admin interface and try to save an object without any value set in this field, django saves a record with blank name.

If I try to edit this object, the link to edit it is my main django project page.

Is this a bug or there is any explaination for this behavior?.

Change History (7)

comment:1 Changed 8 years ago by revil <central@…>

  • Keywords primary_key custom added
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

comment:2 follow-up: Changed 8 years ago by mtredinnick

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

This is not a bug, since an empty string is a valid primary key value (just not a particularly useful one). Please post something to the django-users list if you need more help with this, but using "blank=False" will probably solve your problem.

comment:3 in reply to: ↑ 2 Changed 8 years ago by revil <central@…>

  • Resolution invalid deleted
  • Status changed from closed to reopened

Replying to mtredinnick:

This is not a bug, since an empty string is a valid primary key value (just not a particularly useful one). Please post something to the django-users list if you need more help with this, but using "blank=False" will probably solve your problem.

I agree with you about that an empty string can be used as primary key, but in the django model reference you can read this:

primary_key

If True, this field is the primary key for the model.

If you don't specify primary_key=True for any fields in your model, Django will automatically add this field:

id = models.AutoField('ID', primary_key=True)

Thus, you don't need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior.

primary_key=True implies blank=False, null=False and unique=True. Only one primary key is allowed on an object.

I believe that if setting primary_key to True, implies the previous mentioned statements, then would be not possible to create a blank string primary key.

If I'm wrong, please, explain me what's my misunderstanding.

Thanks for the response.

comment:4 Changed 8 years ago by Michael Radziej <mir@…>

  • Needs tests unset
  • Triage Stage changed from Unreviewed to Design decision needed

revil is right: http://www.djangoproject.com/documentation/model_api/#primary-key. Either the behaviour of primary_key=True needs to change, or the docs.

comment:5 Changed 8 years ago by pacman

This is a serious problem. Is there anything we can do to at least trick the problem?

comment:6 Changed 8 years ago by anonymous

I'm also running into this problem.

What would happen, if we change:

paramsis_required? = not self.blank and not self.primary_key and not rel

to

paramsis_required? = not self.blank and not rel

here? --> http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/__init__.py#L300

comment:7 Changed 7 years ago by lukeplant

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [7713]) Fixed #3774 - primary_key=True does not, in fact, imply blank=False

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