Commits

Mike Bayer committed 617679a

- Class-bound accessor can be used as the argument to
relation() order_by. [ticket:939]

  • Participants
  • Parent commits 9f59e3e
  • Branches rel_0_4

Comments (0)

Files changed (3)

       composite type from being mutated [ticket:1213].
 
     - Added ScopedSession.is_active accessor. [ticket:976]
+    
+    - Class-bound accessor can be used as the argument to 
+      relation() order_by.  [ticket:939]
+      
 - sql
     - Connection.invalidate() checks for closed status 
       to avoid attribute errors. [ticket:1246]

File lib/sqlalchemy/orm/properties.py

         self.target = self.mapper.mapped_table
         self.table = self.mapper.mapped_table
 
+        if self.order_by:
+            self.order_by = [expression._literal_as_column(x) for x in util.to_list(self.order_by)]
+
         if self.cascade.delete_orphan:
             if self.parent.class_ is self.mapper.class_:
                 raise exceptions.ArgumentError("In relationship '%s', can't establish 'delete-orphan' cascade "

File test/ext/declarative.py

         self.assertEquals(a1, Address(email='two'))
         self.assertEquals(a1.user, User(name='u1'))
 
+    def test_eager_order_by(self):
+        class Address(Base, Fixture):
+            __tablename__ = 'addresses'
+
+            id = Column('id', Integer, primary_key=True)
+            email = Column('email', String(50))
+            user_id = Column('user_id', Integer, ForeignKey('users.id'))
+
+        class User(Base, Fixture):
+            __tablename__ = 'users'
+
+            id = Column('id', Integer, primary_key=True)
+            name = Column('name', String(50))
+            addresses = relation("Address", order_by=Address.email)
+
+        Base.metadata.create_all()
+        u1 = User(name='u1', addresses=[
+            Address(email='two'),
+            Address(email='one'),
+        ])
+        sess = create_session()
+        sess.add(u1)
+        sess.flush()
+        sess.clear()
+        self.assertEquals(sess.query(User).options(eagerload(User.addresses)).all(), [User(name='u1', addresses=[
+            Address(email='one'),
+            Address(email='two'),
+        ])])
+
     
     def test_custom_mapper(self):
         class MyExt(MapperExtension):
             assert Foo.__mapper__.compile().extension.create_instance() == 'CHECK'
 
 
-    def test_oops(self):
+    def test_errant_comma(self):
         def define():
             class User(Base, Fixture):
                 __tablename__ = 'users'
             exceptions.ArgumentError,
             "Mapper Mapper|User|users could not assemble any primary key",
             define)
-    test_oops = testing.emits_warning('Ignoring declarative-like tuple value of attribute id')(test_oops)
+    test_errant_comma = testing.emits_warning('Ignoring declarative-like tuple value of attribute id')(test_errant_comma)
 
     def test_expression(self):
         class User(Base, Fixture):