summaryrefslogtreecommitdiff
path: root/test/orm/lazy_relations.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2008-02-09 01:48:19 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2008-02-09 01:48:19 +0000
commit1a3dc519930712d2e6de095a07d76da9a9062210 (patch)
tree2587b92dde7ddba71b369ad1b88c7e0e296470cc /test/orm/lazy_relations.py
parent0b890e1ccde7da937a25107d9287d4dc4e5b7775 (diff)
downloadsqlalchemy-1a3dc519930712d2e6de095a07d76da9a9062210.tar.gz
- lazy loader can now handle a join condition where the "bound"
column (i.e. the one that gets the parent id sent as a bind parameter) appears more than once in the join condition. Specifically this allows the common task of a relation() which contains a parent-correlated subquery, such as "select only the most recent child item". [ticket:946] - col_is_part_of_mappings made more strict, seems to be OK with tests - memusage will dump out the size list in an assertion fail
Diffstat (limited to 'test/orm/lazy_relations.py')
-rw-r--r--test/orm/lazy_relations.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/test/orm/lazy_relations.py b/test/orm/lazy_relations.py
index 4bd3e71e1..55d79fd32 100644
--- a/test/orm/lazy_relations.py
+++ b/test/orm/lazy_relations.py
@@ -7,6 +7,7 @@ from sqlalchemy.orm import *
from testlib import *
from testlib.fixtures import *
from query import QueryTest
+import datetime
class LazyTest(FixtureTest):
keep_mappers = False
@@ -335,5 +336,60 @@ class M2OGetTest(FixtureTest):
assert ad3.user is None
self.assert_sql_count(testing.db, go, 1)
+class CorrelatedTest(ORMTest):
+ keep_mappers = False
+ keep_data = False
+
+ def define_tables(self, meta):
+ global user_t, stuff
+
+ user_t = Table('users', meta,
+ Column('id', Integer, primary_key=True),
+ Column('name', String(50))
+ )
+
+ stuff = Table('stuff', meta,
+ Column('id', Integer, primary_key=True),
+ Column('date', Date),
+ Column('user_id', Integer, ForeignKey('users.id')))
+
+ def insert_data(self):
+ user_t.insert().execute(
+ {'id':1, 'name':'user1'},
+ {'id':2, 'name':'user2'},
+ {'id':3, 'name':'user3'},
+ )
+
+ stuff.insert().execute(
+ {'id':1, 'user_id':1, 'date':datetime.date(2007, 10, 15)},
+ {'id':2, 'user_id':1, 'date':datetime.date(2007, 12, 15)},
+ {'id':3, 'user_id':1, 'date':datetime.date(2007, 11, 15)},
+ {'id':4, 'user_id':2, 'date':datetime.date(2008, 1, 15)},
+ {'id':5, 'user_id':3, 'date':datetime.date(2007, 6, 15)},
+ )
+
+ def test_correlated_lazyload(self):
+ class User(Base):
+ pass
+
+ class Stuff(Base):
+ pass
+
+ mapper(Stuff, stuff)
+
+ stuff_view = select([stuff.c.id]).where(stuff.c.user_id==user_t.c.id).correlate(user_t).order_by(desc(stuff.c.date)).limit(1)
+
+ mapper(User, user_t, properties={
+ 'stuff':relation(Stuff, primaryjoin=and_(user_t.c.id==stuff.c.user_id, stuff.c.id==(stuff_view.as_scalar())))
+ })
+
+ sess = create_session()
+
+ self.assertEquals(sess.query(User).all(), [
+ User(name='user1', stuff=[Stuff(date=datetime.date(2007, 12, 15), id=2)]),
+ User(name='user2', stuff=[Stuff(id=4, date=datetime.date(2008, 1 , 15))]),
+ User(name='user3', stuff=[Stuff(id=5, date=datetime.date(2007, 6, 15))])
+ ])
+
if __name__ == '__main__':
testenv.main()