On 3/4/06, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
>
> OK, it's weirder than you think, though.

Oh.. I don't know... I think it's pretty weird :-)

> I get the same failure whether
> I run all the tests or only Luke's tests using postgreSQL (and
> everything passes with SQLite).

Thanks for the extra detail. It confirmed what I thought was
happening. I've attached a patch which fixes the problem for me, and
I'm pretty sure is a general solution - can anyone (everyone)
interested apply this to their MR tree and check that Luke's test case
(or any other deletion test case you have handy) works for you.

For those interested in the details:

During a deletion, there is a recursive walk of the relations of a
deleted object to find the 'seen objects'. This results in a
dictionary containing {class:instance list} that needs to be deleted.
The actual delete method gets the keys of the dictionary, sorts them
into dependency order, and then deletes them one at a time.

Because the dictionary doesn't keep its keys in a reliable order, the
list presented to be sorted could be in any order. However, there is a
fairly large edge case in the sort algorithm: if class A has no
relation on B, they are declared equal by the sort. However, this
means that if A has no relation on B, and B has no relation on C, but
C is dependent on A, the sort algorithm will not sort [C,B,A] to give
A precedence over C.

This becomes a Heisenbug because the hashcode given to dict keys (and
thus the original order of dict.keys()) is runtime dependent. The more
dependencies/discovered classes you have, the worse the problem gets
(explaining Luke's difficulty in nailing down the problem)

As it happens, the sorting process is redundant anyway. The original
traversal knows the correct deletion order - it's the order in which
objects were discovered. You just have to preserve the discovery order
(i.e., use a SortedDict, not a normal dict).

So - the patch attached fixes some little things with SortedDict, uses
a SortedDict to collate seen_objs, and removes the deletion sort
algorithm.

The problem has been there for a while (pre bulk delete, anyway) -
Luke is just the first person to be bitten by it.

Russ Magee %-)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Attachment: delete-order.patch
Description: Binary data

Reply via email to