CREATE UNIQUE INDEX ab_c_null_idx ON my_table (id_A, id_B) WHERE id_C IS NULL;
I'm wondering if this ought to at least be addressed in a runtime warning, or at least documentation in unique_together -- and I'm hoping that perhaps a Django level workaround could be devised to explicitly ask for unique indexes accommodating null values.
For myself, I'm writing a unittest to fail if any of my unique_together's have a nullable field and using a specific value as my "null" value for now.
Thoughts? Has this come up before?
This is apparently an expected (and standardized) thing in SQL that ('A', 'B', NULL) is unique to ('A', 'B', NULL) as NULL is never equal to another NULL.
There is a workaround at the SQL level of ...CREATE UNIQUE INDEX ab_c_null_idx ON my_table (id_A, id_B) WHERE id_C IS NULL;
I'm wondering if this ought to at least be addressed in a runtime warning,
or at least documentation in unique_together -- and I'm hoping that perhaps a Django level workaround could be devised to explicitly ask for unique indexes accommodating null values.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/566d247e-4aae-429e-9cc3-2544c82ce9a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I am not against a note in the docs, but I find the fact that nulls are not "unique" and can exist in an index more than once very useful (fwiw ordering after a column with null can also be interesting across databases). I'd be interested to hear about your use case -- the "general" use case is usually that you have an optional column but want to ensure it is unique as soon as it is filled…
If you really, really want an unique index that allows just a single
value, you might want to try unique index on (a, b, c) where c is not
null, and another unique index on (a, b) where c is null. That might
give the results you are looking for, though I haven't tested this.
On 29 Apr 2016, at 19:52, Rich Rauenzahn <rrau...@gmail.com> wrote:
I see now that I need to provide a sentinel value -- BOO=True,VAL=<sentinel>, or manually create additional unique indexes.