[sqlalchemy] column_mapped_collection and insertion order preservation

2013-03-10 Thread Massi
Hi everyone,

in my script I'm using column_mapped_collection to create a collection 
indexed by a column of a certain table. This is an example scenario (pseudo 
code):

class Parent(object):
def __init__(self):
 # define some fileds

def AddChild(self, new_child):
 self.children[new_child.name] = new_child

class Child(object):
def __init__(self, name):
self.name = name

rel = relationship(Child, 
collection_class=column_mapped_collection(child_tab.c.name))
mapper(Parent, parent_tab, properties={children:rel})
mapper(Child, child_tab)

p = Parent()
p.AddChild(Child(child1))
p.AddChild(Child(child2))
session.add(p)
session.commit()

Everything works fine except for the fact that after the commit it 
generally happens that the insertion order is not respected, that is the 
child2 record is stored before child1. Is there a way to modify the code so 
that the insertion order is preserved? I think this is somehow linked to 
the use of Ordereddict but I have no idea about how to achieve it!
Any help is appreciated, thanks in advance!

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




Re: [sqlalchemy] column_mapped_collection and insertion order preservation

2013-03-10 Thread Michael Bayer

On Mar 10, 2013, at 10:28 AM, Massi massi_...@msn.com wrote:

 Hi everyone,
 
 in my script I'm using column_mapped_collection to create a collection 
 indexed by a column of a certain table. This is an example scenario (pseudo 
 code):
 
 class Parent(object):
 def __init__(self):
  # define some fileds
 
 def AddChild(self, new_child):
  self.children[new_child.name] = new_child
 
 class Child(object):
 def __init__(self, name):
 self.name = name
 
 rel = relationship(Child, 
 collection_class=column_mapped_collection(child_tab.c.name))
 mapper(Parent, parent_tab, properties={children:rel})
 mapper(Child, child_tab)
 
 p = Parent()
 p.AddChild(Child(child1))
 p.AddChild(Child(child2))
 session.add(p)
 session.commit()
 
 Everything works fine except for the fact that after the commit it generally 
 happens that the insertion order is not respected, that is the child2 record 
 is stored before child1. Is there a way to modify the code so that the 
 insertion order is preserved? I think this is somehow linked to the use of 
 Ordereddict but I have no idea about how to achieve it!
 Any help is appreciated, thanks in advance!

OK in this case, it's true that using an OrderedDict would help.  There is a 
way to map to an OrderedDict here, though it's not very convenient as you'd 
need to build it either from scratch or by subclassing the existing 
MappedCollection class, and then there's some other boilerplate-ish/not really 
public utilities that the attribute_mapped_collection and 
column_mapped_collection callables make use of, though they aren't strictly 
necessary.   

But all of that can be bypassed if you're OK with being more specific as to how 
you put the objects in the Session.  Ultimately, insert order is determined by 
the order in which the objects were put into the Session.  So here, if you were 
to session.add(p) *first*, then add the Child objects, each one gets cascaded 
into the Session at the moment you add them to the collection, and that's the 
insert order.  Or, if you session.add() each child *before* it goes into the 
Session via session.add(p), that will also work. 



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

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