I will preface this by noting that I didn't write orderinglist and I've also 
haven't had the chance to use it in a project.

One thing I had to spend quite awhile on here is what exactly the problem is.   
Using reorder_on_append=True, doing away with the extension, I can't get it to 
screw up the ordering of the elements.   

Not knowing what the problem is,  I theorized you are wanting the "position" to 
be sequential.   This isn't a guarantee of orderinglist and is not really 
something you'd want to be concerned about typically.   

But let's say that's what we're going for.    delete(obj) will not remove the 
object from collections - OTOH if you remove each Item from a parent collection 
using collection.remove(), that gives the orderinglist a chance to maintain the 
ordering.

There are some tricky behaviors introduced by orderinglist - I'm noticing that 
in the case of the out-of-collection delete(),  just loading a collection 
triggers append() events which then reset the position element.   This suggests 
further that you might want to maintain those collections before emitting the 
delete(), as otherwise it does seem to come in and reorder the numbering as a 
side effect of other operations.

If the purpose of the extension is to do exactly that, then you should probably 
use a SessionExtension.before_flush(), since that's where you can do things 
that can affect the flush plan - orderinglist's rearrangement of .position 
attributes counts as something that affects the flush plan (makes ItemOrder 
objects dirty that were not before).

I worked up a fairly uncomfortable one that seems to work:

class ReorderCollection(SessionExtension):
    def before_flush( self, session, flush_context, instances):
        for instance in session.deleted:
            if isinstance(instance, Item):
                for io in list(instance._item_orders):
                    if io.item is instance:
                        io.collection._item_orders.remove(io)

Session = sessionmaker(bind=engine, extension=ReorderCollection())

There might be a better way to organize that as I don't have time to keep 
playing with it,  but beyond that I'd advise not assigning meaning to the 
.position element other than a means of sorting.

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

Reply via email to