Django

Code

Ticket #4136 (new)

Opened 3 years ago

Last modified 1 year ago

NULL fields and Unique keys

Reported by: David Cramer <dcramer@gmail.com> Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: 0.96 Keywords:
Cc: listuser@peternixon.net Triage Stage: Accepted
Has patch: 0 Needs documentation: 1
Needs tests: 0 Patch needs improvement: 0

Description

When Django sends an update to MySQL for a NULL field it does not insert it as NULL (when you are in the admin).

e.g. You have an openid_key field, which is the url of the users openid url. This url has to be unique and should be marked as such for db integrity.

You go into the admin and edit a user, they have no openid_key. You save the user. All is fine.

You go into the admin and edit a different user, they have no openid_key. You save the user. SQL throws a fit about the key needs to be unique.

Django tried to insert "" instead of NULL for this field.

Attachments

Change History

04/23/07 19:51:48 changed by Ramiro Morales

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

What options are you using for the openid_key field?, is it a CharField? a ForeignKey?. Please post [a fragment with relevant parts to reproduce the problem of] your models.py file to the django-users mailing list.

04/23/07 20:03:58 changed by David Cramer <dcramer@gmail.com>

It's a URLField (as it's a URL), but it shouldnt matter what kind of field it is.

04/23/07 23:27:15 changed by ubernostrum

Hm. The problem here is that fields which -- in Python -- reduce to a string value have null input stored as empty strings instead of as NULL (this is mentioned in the model docs), and with good reason; as the docs point out, allowing both NULL and an empty string creates ambiguity. I'm not sure what the best solution is here in the face of a nullable but unique field which holds a string.

04/23/07 23:42:50 changed by David Cramer <dcramer@gmail.com>

Being that this is supposed to be on top of a DB layer I think a string should be treated as NULL when it's blank. At least if the field is set to null=True. In reality a string being is the same as being empty. I have no problems with this except for the admin interface.

04/23/07 23:45:39 changed by mtredinnick

  • needs_docs set to 1.
  • stage changed from Unreviewed to Accepted.

We are not going to interpret empty strings as NULL. They are not the same thing, in general.

I think this is something that needs to be taken care of in the model's save() method or as the data is cleaned (converted from form to Python objects). Not closing yet, because we can probably come up with some documentation and best practices here, but we aren't going to start automatically storing NULLs.

10/10/07 06:50:53 changed by nix

  • cc set to listuser@peternixon.net.

Malcolm

I have to respectfully disagree with you on this issue. I have the following in a model:

mac_address = models.CharField?(maxlength=12, blank=True, null=True, unique=True)

This causes the admin interface to throw database constraint errors whenever there is more than one empty mac address in the table. If the admin interface correctly left these as NULL then everything would work properly. If you can tell me a better way to get the behaviour I need then I am open to suggestions (Field should be blank or unique) as I use this for a large number of fields and the fact that it works properly on everything except strings is very frustrating.

11/24/07 19:52:06 changed by SmileyChris

  • component changed from Database wrapper to Admin interface.

This seems more of an admin problem than a db wrapper problem. The db handles it fine (as far as I know) if you actually set the value to be None, it is just the fact that the admin can't tell (or accurately display) the difference between a blank string and null.

I think newforms-admin should have a special NullCharField widget for db CharFields which have null=True with two radio options, the first with a label of "None" and the second with the text box. This way, you can make the differentiation between a null and blank string.

07/29/09 14:30:38 changed by adamnelson

I tried this model definition:

class Tester(models.Model):

mac_address = models.CharField?(max_length=12, blank=True, null=True, default=None, unique=True)

The admin did indeed give a duplicate key error when trying to add a blank value twice. With a default of None (Null), I think the admin should submit None for a blank field, and therefore no key constraint should hit (since None != None or Null != Null). In the case where default is not set or is not None then I think an empty string is the correct value.


Add/Change #4136 (NULL fields and Unique keys)




Change Properties
Action