[sqlalchemy] column_mapped_collection and insertion order preservation
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] window functions marked deferred
On 03/08/2013 18:09, Michael Bayer wrote: On Mar 8, 2013, at 5:31 AM, Julien Cigar jci...@ulb.ac.be wrote: Hello, Shouldn't SQLAlchemy emit (at least) a warning when a window function is mapped with an orm.column_property(), marked deferred=True, and accessed without query.options(orm.undefer()) context (.. or maybe it's the role of the dev to call it in a non-deferred context?) ? For example: 'row_number' : orm.column_property( sql.func.row_number().\ over(partition_by=table['content'].c.container_id, order_by=table['content'].c.weight.desc()), deferred = True ) will always return 1 in a deferred context .. trying to catch that scenario seems a bit arbitrary to me, there's all kinds of SQL that won't work as a deferred column property, and how do we know that particular SQL is not what's intended ? You're right .. it was a silly idea (: -- 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
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.