Opened 18 years ago

Closed 17 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: dev
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: no UI/UX: no

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 by revil <central@…>, 18 years ago

Keywords: primary_key custom added
Needs tests: set

comment:2 by Malcolm Tredinnick, 18 years ago

Resolution: invalid
Status: newclosed

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.

in reply to:  2 comment:3 by revil <central@…>, 18 years ago

Resolution: invalid
Status: closedreopened

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 by Michael Radziej <mir@…>, 18 years ago

Needs tests: unset
Triage Stage: UnreviewedDesign 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 by pacman, 17 years ago

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

comment:6 by anonymous, 17 years ago

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 by Luke Plant, 17 years ago

Resolution: fixed
Status: reopenedclosed

(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