Jim,<div><br></div><div>On Tue, Nov 20, 2012 at 10:26 AM,  <span dir="ltr"><<a href="mailto:jep200404@columbus.rr.com" target="_blank">jep200404@columbus.rr.com</a>></span> wrote:<br></div><div class="gmail_extra"><div class="gmail_quote">


<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">What's a cleaner way of accomplishing the unique_together stuff,<br>



hopefully without the (redundant) previous_id_num attribute?<br>
<br>
I'm using Postgresql with Django. Postgresql's implementation of<br>
unique constraints when a value is NULL (None in Python) does not<br>
work as desired. I figured out an ugly workaround. Hopefully,<br>
you know a better way.<br></blockquote><div><br></div><div><br></div><div>Null in a database sense means "missing data", and the SQL standard specifically states that two NULL values are not equal. That's why the unique-together stuff doesn't work as expected.  The thought being that since the data point is "unknown" you can't ascribe equality to any two unknown data points. For more than you ever wanted to know about NULL, there is a fairly comprehensive Wikipedia article [1].<br>


</div><div><br></div><div>Now you might say, when I do group by or ordering in SQL, the NULL's are grouped and ordered together.  How can that be in NULL != NULL? The SQL standard gets around that by defining group by and order by as putting "not distinct" items together.  And SQL defines "any two values that are equal to one another, or any two Nulls" as being "not distinct". So while NULL does not equal NULL, they are indistinct from one another as far as SQL goes.</div>

<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">An (ugly) thought is to not use None/NULL values for previous to<br>



indicate end of linked list.<br>
Instead one would set up a sentinel dummy Book record for the end of<br>
a linked list. That would make unique_constraint happy, but push<br>
ugliness into the code that groks the linked list.<br></blockquote><div><br></div><div><br></div><div>Not ugly at all, that is a standard way to do that in a relational database.  Another common way is to use an order or positional field as part of the unique-together rather than the index field. It would start at 1 (or 0) for the first item in the list, and increment as you add items.  That works best if you are just adding to the end of the list, not reordering, and it involves adding another field to the table and is another field to maintain.</div>

<div><br></div><div>[1] <a href="http://en.wikipedia.org/wiki/Null_(SQL)" target="_blank">http://en.wikipedia.org/wiki/Null_(SQL)</a></div><div><br></div><div>-Eric</div><div><br></div></div></div>