Discussion:
unique_together and null=True fields
Wanrong Lin
2008-07-02 03:10:01 UTC
Permalink
Hi,

This topic has been discussed before here:
http://groups.google.com/group/django-users/browse_thread/thread/fcd04f766b101a95/b65b70aa6b03729e?lnk=gst&q=unique_together%2C+null#

Don't you agree that two field combinations like ("A", null) and ("A",
null) should be considered identical and hence should be blocked from
being entered into the database when "unique_together" condition is
imposed on that field combination? Right now Django does not think so
and will let both of those two records get into the database, which is
really puzzling to me. Any rationale for such a counter-intuitive (to
me) behavior?

Thank you for shedding your light on it.

Wanrong



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Malcolm Tredinnick
2008-07-02 03:18:16 UTC
Permalink
Post by Wanrong Lin
Hi,
http://groups.google.com/group/django-users/browse_thread/thread/fcd04f766b101a95/b65b70aa6b03729e?lnk=gst&q=unique_together%2C+null#
Don't you agree that two field combinations like ("A", null) and ("A",
null) should be considered identical and hence should be blocked from
being entered into the database when "unique_together" condition is
imposed on that field combination? Right now Django does not think so
and will let both of those two records get into the database, which is
really puzzling to me. Any rationale for such a counter-intuitive (to
me) behavior?
In SQL, all NULLs are unique (NULL != NULL). So this isn't
counter-intuitive at the SQL level and unique_together is enforced at
that level.

I guess the solution is not to allow NULLs in those fields if you can
avoid it, or manually add a more complex check constraint to the
database table (exact syntax and format will depend upon your database).

Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...