Admin refuses multiple NULL values for fields marked unique
|Reported by:||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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.