[sqlalchemy] Re: relation does not honor order_by attribute
On Wed, Oct 14, 2009 at 08:34, robert rottermann rob...@redcor.ch wrote: I am using a one to many relation, and would like the result to be ordered by a field of the child table. however no order by statement is executed when I access the related property of the parent object. this is my declaration: mitarbeiter_table = Table('mitarbeiter', Base2.metadata, autoload=True) class mitarbeiterCL(Base2): __table__ = mitarbeiter_table abwesenheit_table = Table('abwesenheit', Base2.metadata, autoload=True) class abwesenheitCL(Base2): __table__ = abwesenheit_table mitarbeiter = relation( mitarbeiterCL, uselist=False, backref='abwesenheiten', order_by = abwesenheit_table.c.datumvon, ) The problem is that you are specifying the order_by on the wrong side of the relationship (ie on the ManyToOne side). You need to place the order_by on the OneToMany side, and in your precise example, that means, on the backref. mitarbeiter = relation( mitarbeiterCL, uselist=False, backref=backref('abwesenheiten', order_by=abwesenheit_table.c.datumvon) ) -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: relation does not honor order_by attribute
thanks robert Gaetan de Menten schrieb: On Wed, Oct 14, 2009 at 08:34, robert rottermann rob...@redcor.ch wrote: I am using a one to many relation, and would like the result to be ordered by a field of the child table. however no order by statement is executed when I access the related property of the parent object. this is my declaration: mitarbeiter_table = Table('mitarbeiter', Base2.metadata, autoload=True) class mitarbeiterCL(Base2): __table__ = mitarbeiter_table abwesenheit_table = Table('abwesenheit', Base2.metadata, autoload=True) class abwesenheitCL(Base2): __table__ = abwesenheit_table mitarbeiter = relation( mitarbeiterCL, uselist=False, backref='abwesenheiten', order_by = abwesenheit_table.c.datumvon, ) The problem is that you are specifying the order_by on the wrong side of the relationship (ie on the ManyToOne side). You need to place the order_by on the OneToMany side, and in your precise example, that means, on the backref. mitarbeiter = relation( mitarbeiterCL, uselist=False, backref=backref('abwesenheiten', order_by=abwesenheit_table.c.datumvon) ) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---