]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed indexing of Query objects by -1. It was erroneously
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 15 Nov 2010 14:55:43 +0000 (09:55 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 15 Nov 2010 14:55:43 +0000 (09:55 -0500)
transformed to the empty slice -1:0 that resulted in
IndexError. [ticket:1968]

CHANGES
lib/sqlalchemy/orm/query.py
test/orm/test_generative.py

diff --git a/CHANGES b/CHANGES
index 7f15effc1674b425760a685bad7339413d1ac10f..a6fd7009ec5ddead0fa4911b753c433955b02d6b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -24,6 +24,10 @@ CHANGES
     strategy would fail if the entity was an aliased()
     construct.  [ticket:1964]
 
+  - Fixed indexing of Query objects by -1. It was erroneously
+    transformed to the empty slice -1:0 that resulted in
+    IndexError. [ticket:1968]
+
 - sql
   - The 'info' attribute of Column is copied during 
     Column.copy(), i.e. as occurs when using columns
index 468bcc19d61a19be646efc1cc41b1e2746c22afa..2bccb8f73fadc45e6b8c4b16fd3007f482a5b5a8 100644 (file)
@@ -1527,7 +1527,10 @@ class Query(object):
             else:
                 return list(res)
         else:
-            return list(self[item:item+1])[0]
+            if item == -1:
+                return list(self)[-1]
+            else:
+                return list(self[item:item+1])[0]
 
     @_generative(_no_statement_condition)
     def slice(self, start, stop):
index 141fde9fc65c263d288f9932b1de321d09aaaffe..31b7be8cc0569c43548d2ba3a3e38f6ad7fb20a6 100644 (file)
@@ -48,6 +48,9 @@ class GenerativeQueryTest(_base.MappedTest):
         orig = query.all()
         
         assert query[1] == orig[1]
+        assert query[-4] == orig[-4]
+        assert query[-1] == orig[-1]
+        
         assert list(query[10:20]) == orig[10:20]
         assert list(query[10:]) == orig[10:]
         assert list(query[:10]) == orig[:10]
@@ -77,12 +80,12 @@ class GenerativeQueryTest(_base.MappedTest):
         assert query.filter(foo.c.bar<30).values(sa.func.max(foo.c.bar)).next()[0] == 29
         assert query.filter(foo.c.bar<30).values(sa.func.max(foo.c.bar)).next()[0] == 29
         # end Py2K
-        
+    
+    @testing.fails_if(lambda:testing.against('mysql+mysqldb') and
+            testing.db.dialect.dbapi.version_info[:4] == (1, 2, 1, 'gamma'),
+            "unknown incompatibility")
     @testing.resolve_artifact_names
     def test_aggregate_1(self):
-        if (testing.against('mysql+mysqldb') and
-            testing.db.dialect.dbapi.version_info[:4] == (1, 2, 1, 'gamma')):
-            return
 
         query = create_session().query(func.sum(foo.c.bar))
         assert query.filter(foo.c.bar<30).one() == (435,)