Django

Code

Ticket #3774 (closed: fixed)

Opened 1 year ago

Last modified 2 months ago

Custom primary key allows to record a null or blank value

Reported by: revil Assigned to: nobody
Milestone: Component: Database wrapper
Version: SVN Keywords: primary_key,custom primary key
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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?.

Attachments

Change History

03/20/07 22:28:34 changed by revil <central@evosistemas.com>

  • keywords changed from primary,key to primary_key,custom primary key.
  • needs_better_patch changed.
  • needs_tests set to 1.
  • needs_docs changed.

(follow-up: ↓ 3 ) 03/20/07 22:34:00 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to invalid.

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 ) 03/20/07 22:56:24 changed by revil <central@evosistemas.com>

  • status changed from closed to reopened.
  • resolution deleted.

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.

03/21/07 01:32:40 changed by Michael Radziej <mir@noris.de>

  • needs_tests deleted.
  • 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.

07/19/07 10:07:20 changed by pacman

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

01/16/08 00:06:44 changed 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

06/20/08 07:10:14 changed by lukeplant

  • status changed from reopened to closed.
  • resolution set to fixed.

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


Add/Change #3774 (Custom primary key allows to record a null or blank value)




Change Properties
Action