]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add the uselist=False / single row assertion from [ticket:1643] for lazy loads too.
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 19 Dec 2009 05:41:37 +0000 (05:41 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 19 Dec 2009 05:41:37 +0000 (05:41 +0000)
CHANGES
lib/sqlalchemy/orm/strategies.py
test/orm/test_lazy_relations.py
test/orm/test_relationships.py

diff --git a/CHANGES b/CHANGES
index aa8fc73c0ebe50be879e649f11d94ed90fb60bd9..94540138180476f0998f9cbf0fcf8e200fdac483 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -70,9 +70,10 @@ CHANGES
     [ticket:1531]
 
   - relation() with uselist=False will emit a warning when 
-    an eager load locates more than one valid value for the row,
-    typically due to primaryjoin/secondaryjoin conditions which
-    aren't appropriate for LEFT OUTER JOIN.  [ticket:1643]
+    an eager or lazy load locates more than one valid value for 
+    the row.  This may be due to primaryjoin/secondaryjoin 
+    conditions which aren't appropriate for an eager LEFT OUTER 
+    JOIN or for other conditions.  [ticket:1643]
     
   - an explicit check occurs when a synonym() is used with 
     map_column=True, when a ColumnProperty (deferred or otherwise)
index 01da0f23a4703bc929947f0c41eb316322a3524f..ea00d605d3f0d8c7494daf81f3e82eedc23e0ea4 100644 (file)
@@ -600,7 +600,13 @@ class LoadLazyAttribute(object):
         if strategy.uselist:
             return result
         else:
-            if result:
+            l = len(result)
+            if l:
+                if l > 1:
+                    util.warn(
+                        "Multiple rows returned with "
+                        "uselist=False for lazily-loaded attribute '%s' " % prop)
+                    
                 return result[0]
             else:
                 return None
index 1cdd8213005308dc813ed76ec7ed5ec2023436aa..1104ef1e0a974bbb2e0c08a73d27a3ec5f1407f5 100644 (file)
@@ -167,6 +167,18 @@ class LazyTest(_fixtures.FixtureTest):
         l = q.filter(s.c.u2_id==User.id).order_by(User.id).distinct().all()
         eq_(self.static.user_all_result, l)
 
+    @testing.resolve_artifact_names 
+    def test_uselist_false_warning(self):
+        """test that multiple rows received by a uselist=False raises a warning."""
+
+        mapper(User, users, properties={
+            'order':relation(Order, uselist=False)
+        })
+        mapper(Order, orders)
+        s = create_session()
+        u1 = s.query(User).filter(User.id==7).one()
+        assert_raises(sa.exc.SAWarning, getattr, u1, 'order')
+
     @testing.resolve_artifact_names
     def test_one_to_many_scalar(self):
         mapper(User, users, properties = dict(
index 63d66b7428882ff19a5823aad262818cd07c1447..443b1c93b853d0d2f7373160aeaa1b101f870eeb 100644 (file)
@@ -329,7 +329,7 @@ class RelationTest3(_base.MappedTest):
                                  order_by=pages.c.pagename)),
             'currentversion': relation(
                  PageVersion,
-                 foreign_keys=[pages.c.current_version],
+                 uselist=False,
                  primaryjoin=sa.and_(
                      pages.c.jobno==pageversions.c.jobno,
                      pages.c.pagename==pageversions.c.pagename,
@@ -353,7 +353,7 @@ class RelationTest3(_base.MappedTest):
                                   order_by=pagecomments.c.comment_id))})
 
     @testing.resolve_artifact_names
-    def testbasic(self):
+    def test_basic(self):
         """A combination of complicated join conditions with post_update."""
 
         j1 = Job(jobno=u'somejob')