From: Mike Bayer Date: Sat, 19 Dec 2009 05:41:37 +0000 (+0000) Subject: add the uselist=False / single row assertion from [ticket:1643] for lazy loads too. X-Git-Tag: rel_0_6beta1~119 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0446d17a3e5811e0a459d878bcac3a33cafe5753;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git add the uselist=False / single row assertion from [ticket:1643] for lazy loads too. --- diff --git a/CHANGES b/CHANGES index aa8fc73c0e..9454013818 100644 --- 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) diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index 01da0f23a4..ea00d605d3 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -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 diff --git a/test/orm/test_lazy_relations.py b/test/orm/test_lazy_relations.py index 1cdd821300..1104ef1e0a 100644 --- a/test/orm/test_lazy_relations.py +++ b/test/orm/test_lazy_relations.py @@ -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( diff --git a/test/orm/test_relationships.py b/test/orm/test_relationships.py index 63d66b7428..443b1c93b8 100644 --- a/test/orm/test_relationships.py +++ b/test/orm/test_relationships.py @@ -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')