Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#9781 closed (invalid)

Admin refuses multiple NULL values for fields marked unique

Reported by: Daniel Pope <dan@…> Owned by: nobody
Component: contrib.admin Version: 1.0
Severity: Keywords: null, unique
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In all of Django's supported DMBSes, the UNIQUE constraint does not apply to NULL values. This allows the database to represent data of the form "This must be unique where it is set". The Django admin site does not reflect this constraint properly: it treats NULL values as duplicates for the purposes of validation, showing an unwanted error stating that it must be unique.

This behaviour is not universal in DBMSes. Documentation of the capabilities of NULLs in various DBMSes is here: http://www.sqlite.org/nulls.html (This behaviour is "nulls are distinct in a UNIQUE column: Yes"). However, as that page states, there are good reasons why this is done, and the convention has been adopted by all except MSSQL and Informix (and some databases constrain the column specification more strictly so that the issue doesn't arise).

There are several use cases where this provides extremely useful functionality, for example:

show_on_page = models.IntegerField(choices=[(1, 'Home page'), (2, 'Contact Page')], blank=True, null=True, unique=True)

should allow one instance of the model for the home page, one for the contact page, and any number of other instances where show_on_page is NULL.

The admin gives no way of entering a NULL value for a string field but this should work for IntegerFields etc.

Change History (2)

comment:1 Changed 7 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This was a problem in 1.0 but it has been fixed for over a month, see #9039. Fix included tests and the buildbot is passing all tests on current code so I don't think the the fix has been broken. Are you saying you see problems on current (or even latest release 1.0.2 code)? If so we'll need specifics of what exactly you are doing.

comment:2 Changed 7 years ago by Daniel Pope <dan@…>

Indeed, this was with 1.0. I searched the tracker but didn't find #9039. Thanks.

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