summaryrefslogtreecommitdiff
path: root/test/orm/test_assorted_eager.py
blob: b3fa35a6fa9f360af40ce39ed36823289b5ca5a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
"""Exercises for eager loading.

Derived from mailing list-reported problems and issue tracker issues.

These are generally very old 0.1-era tests and at some point should
be cleaned up and modernized.

"""
import datetime

import sqlalchemy as sa
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy.orm import backref
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.fixtures import fixture_session
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table


class EagerTest(fixtures.MappedTest):
    run_deletes = None
    run_inserts = "once"
    run_setup_mappers = "once"

    @classmethod
    def define_tables(cls, metadata):

        Table(
            "owners",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("data", String(30)),
        )

        Table(
            "categories",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("name", String(20)),
        )

        Table(
            "tests",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column(
                "owner_id", Integer, ForeignKey("owners.id"), nullable=False
            ),
            Column(
                "category_id",
                Integer,
                ForeignKey("categories.id"),
                nullable=False,
            ),
        )

        Table(
            "options",
            metadata,
            Column(
                "test_id", Integer, ForeignKey("tests.id"), primary_key=True
            ),
            Column(
                "owner_id", Integer, ForeignKey("owners.id"), primary_key=True
            ),
            Column(
                "someoption",
                sa.Boolean,
                server_default=sa.false(),
                nullable=False,
            ),
        )

    @classmethod
    def setup_classes(cls):
        class Owner(cls.Basic):
            pass

        class Category(cls.Basic):
            pass

        class Thing(cls.Basic):
            pass

        class Option(cls.Basic):
            pass

    @classmethod
    def setup_mappers(cls):
        Category, owners, Option, tests, Thing, Owner, options, categories = (
            cls.classes.Category,
            cls.tables.owners,
            cls.classes.Option,
            cls.tables.tests,
            cls.classes.Thing,
            cls.classes.Owner,
            cls.tables.options,
            cls.tables.categories,
        )

        cls.mapper_registry.map_imperatively(Owner, owners)

        cls.mapper_registry.map_imperatively(Category, categories)

        cls.mapper_registry.map_imperatively(
            Option,
            options,
            properties=dict(
                owner=relationship(Owner, viewonly=True),
                test=relationship(Thing, viewonly=True),
            ),
        )

        cls.mapper_registry.map_imperatively(
            Thing,
            tests,
            properties=dict(
                owner=relationship(Owner, backref="tests"),
                category=relationship(Category),
                owner_option=relationship(
                    Option,
                    primaryjoin=sa.and_(
                        tests.c.id == options.c.test_id,
                        tests.c.owner_id == options.c.owner_id,
                    ),
                    foreign_keys=[options.c.test_id, options.c.owner_id],
                    uselist=False,
                ),
            ),
        )

    @classmethod
    def insert_data(cls, connection):
        Owner, Category, Option, Thing = (
            cls.classes.Owner,
            cls.classes.Category,
            cls.classes.Option,
            cls.classes.Thing,
        )

        session = Session(connection)

        o = Owner()
        c = Category(name="Some Category")
        session.add_all(
            (
                Thing(owner=o, category=c),
                Thing(
                    owner=o, category=c, owner_option=Option(someoption=True)
                ),
                Thing(owner=o, category=c, owner_option=Option()),
            )
        )

        session.flush()

    def test_noorm(self, connection):
        """test the control case"""

        tests, options, categories = (
            self.tables.tests,
            self.tables.options,
            self.tables.categories,
        )

        # I want to display a list of tests owned by owner 1
        # if someoption is false or they haven't specified it yet (null)
        # but not if they set it to true (example someoption is for hiding)

        # desired output for owner 1
        # test_id, cat_name
        # 1 'Some Category'
        # 3  "

        # not orm style correct query
        print("Obtaining correct results without orm")
        result = connection.execute(
            sa.select(tests.c.id, categories.c.name)
            .where(
                sa.and_(
                    tests.c.owner_id == 1,
                    sa.or_(
                        options.c.someoption == None,  # noqa
                        options.c.someoption == False,
                    ),
                )
            )
            .order_by(tests.c.id)
            .select_from(
                tests.join(categories).outerjoin(
                    options,
                    sa.and_(
                        tests.c.id == options.c.test_id,
                        tests.c.owner_id == options.c.owner_id,
                    ),
                )
            )
        ).fetchall()
        eq_(result, [(1, "Some Category"), (3, "Some Category")])

    def test_withoutjoinedload(self):
        Thing, tests, options = (
            self.classes.Thing,
            self.tables.tests,
            self.tables.options,
        )

        s = fixture_session()
        result = (
            s.query(Thing)
            .select_from(
                tests.outerjoin(
                    options,
                    sa.and_(
                        tests.c.id == options.c.test_id,
                        tests.c.owner_id == options.c.owner_id,
                    ),
                )
            )
            .filter(
                sa.and_(
                    tests.c.owner_id == 1,
                    sa.or_(
                        options.c.someoption == None,  # noqa
                        options.c.someoption == False,
                    ),
                )
            )
        )

        result_str = ["%d %s" % (t.id, t.category.name) for t in result]
        eq_(result_str, ["1 Some Category", "3 Some Category"])

    def test_withjoinedload(self):
        """
        Test that an joinedload locates the correct "from" clause with which to
        attach to, when presented with a query that already has a complicated
        from clause.

        """

        Thing, tests, options = (
            self.classes.Thing,
            self.tables.tests,
            self.tables.options,
        )

        s = fixture_session()
        q = s.query(Thing).options(sa.orm.joinedload(Thing.category))

        result = q.select_from(
            tests.outerjoin(
                options,
                sa.and_(
                    tests.c.id == options.c.test_id,
                    tests.c.owner_id == options.c.owner_id,
                ),
            )
        ).filter(
            sa.and_(
                tests.c.owner_id == 1,
                sa.or_(
                    options.c.someoption == None,
                    options.c.someoption == False,  # noqa
                ),
            )
        )

        result_str = ["%d %s" % (t.id, t.category.name) for t in result]
        eq_(result_str, ["1 Some Category", "3 Some Category"])

    def test_dslish(self):
        """test the same as withjoinedload except using generative"""

        Thing, tests, options = (
            self.classes.Thing,
            self.tables.tests,
            self.tables.options,
        )

        s = fixture_session()
        q = s.query(Thing).options(sa.orm.joinedload(Thing.category))
        result = q.filter(
            sa.and_(
                tests.c.owner_id == 1,
                sa.or_(
                    options.c.someoption == None,
                    options.c.someoption == False,  # noqa
                ),
            )
        ).outerjoin(Thing.owner_option)

        result_str = ["%d %s" % (t.id, t.category.name) for t in result]
        eq_(result_str, ["1 Some Category", "3 Some Category"])

    def test_without_outerjoin_literal(self):
        Thing, tests = (self.classes.Thing, self.tables.tests)

        s = fixture_session()
        q = s.query(Thing).options(sa.orm.joinedload(Thing.category))
        result = q.filter(
            (tests.c.owner_id == 1)
            & text(
                "options.someoption is null or options.someoption=:opt"
            ).bindparams(opt=False)
        ).join(Thing.owner_option)

        result_str = ["%d %s" % (t.id, t.category.name) for t in result]
        eq_(result_str, ["3 Some Category"])

    def test_withoutouterjoin(self):
        Thing, tests, options = (
            self.classes.Thing,
            self.tables.tests,
            self.tables.options,
        )

        s = fixture_session()
        q = s.query(Thing).options(sa.orm.joinedload(Thing.category))
        result = q.filter(
            (tests.c.owner_id == 1)
            & (
                (options.c.someoption == None)
                | (options.c.someoption == False)
            )  # noqa
        ).join(Thing.owner_option)

        result_str = ["%d %s" % (t.id, t.category.name) for t in result]
        eq_(result_str, ["3 Some Category"])


class EagerTest2(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "left",
            metadata,
            Column("id", Integer, ForeignKey("middle.id"), primary_key=True),
            Column("data", String(50), primary_key=True),
        )

        Table(
            "middle",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("data", String(50)),
        )

        Table(
            "right",
            metadata,
            Column("id", Integer, ForeignKey("middle.id"), primary_key=True),
            Column("data", String(50), primary_key=True),
        )

    @classmethod
    def setup_classes(cls):
        class Left(cls.Basic):
            def __init__(self, data):
                self.data = data

        class Middle(cls.Basic):
            def __init__(self, data):
                self.data = data

        class Right(cls.Basic):
            def __init__(self, data):
                self.data = data

    @classmethod
    def setup_mappers(cls):
        Right, Middle, middle, right, left, Left = (
            cls.classes.Right,
            cls.classes.Middle,
            cls.tables.middle,
            cls.tables.right,
            cls.tables.left,
            cls.classes.Left,
        )

        # set up bi-directional eager loads
        cls.mapper_registry.map_imperatively(Left, left)
        cls.mapper_registry.map_imperatively(Right, right)
        cls.mapper_registry.map_imperatively(
            Middle,
            middle,
            properties=dict(
                left=relationship(
                    Left,
                    lazy="joined",
                    backref=backref("middle", lazy="joined"),
                ),
                right=relationship(
                    Right,
                    lazy="joined",
                    backref=backref("middle", lazy="joined"),
                ),
            ),
        ),

    def test_eager_terminate(self):
        """Eager query generation does not include the same mapper's table
        twice.

        Or, that bi-directional eager loads don't include each other in eager
        query generation.

        """

        Middle, Right, Left = (
            self.classes.Middle,
            self.classes.Right,
            self.classes.Left,
        )

        p = Middle("m1")
        p.left.append(Left("l1"))
        p.right.append(Right("r1"))

        session = fixture_session()
        session.add(p)
        session.flush()
        session.expunge_all()
        session.query(Left).filter_by(data="l1").one()


class EagerTest3(fixtures.MappedTest):
    """Eager loading combined with nested SELECT statements, functions, and
    aggregates."""

    @classmethod
    def define_tables(cls, metadata):
        Table(
            "datas",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("a", Integer, nullable=False),
        )

        Table(
            "foo",
            metadata,
            Column(
                "data_id", Integer, ForeignKey("datas.id"), primary_key=True
            ),
            Column("bar", Integer),
        )

        Table(
            "stats",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("data_id", Integer, ForeignKey("datas.id")),
            Column("somedata", Integer, nullable=False),
        )

    @classmethod
    def setup_classes(cls):
        class Data(cls.Basic):
            pass

        class Foo(cls.Basic):
            pass

        class Stat(cls.Basic):
            pass

    def test_nesting_with_functions(self):
        Stat, Foo, stats, foo, Data, datas = (
            self.classes.Stat,
            self.classes.Foo,
            self.tables.stats,
            self.tables.foo,
            self.classes.Data,
            self.tables.datas,
        )

        self.mapper_registry.map_imperatively(Data, datas)
        self.mapper_registry.map_imperatively(
            Foo,
            foo,
            properties={
                "data": relationship(
                    Data, backref=backref("foo", uselist=False)
                )
            },
        )

        self.mapper_registry.map_imperatively(
            Stat, stats, properties={"data": relationship(Data)}
        )

        session = fixture_session()

        data = [Data(a=x) for x in range(5)]
        session.add_all(data)

        session.add_all(
            (
                Stat(data=data[0], somedata=1),
                Stat(data=data[1], somedata=2),
                Stat(data=data[2], somedata=3),
                Stat(data=data[3], somedata=4),
                Stat(data=data[4], somedata=5),
                Stat(data=data[0], somedata=6),
                Stat(data=data[1], somedata=7),
                Stat(data=data[2], somedata=8),
                Stat(data=data[3], somedata=9),
                Stat(data=data[4], somedata=10),
            )
        )
        session.flush()

        arb_data = (
            sa.select(
                stats.c.data_id, sa.func.max(stats.c.somedata).label("max")
            )
            .where(stats.c.data_id <= 5)
            .group_by(stats.c.data_id)
        )

        arb_result = session.connection().execute(arb_data).fetchall()

        # order the result list descending based on 'max'
        arb_result.sort(key=lambda a: a._mapping["max"], reverse=True)

        # extract just the "data_id" from it
        arb_result = [row._mapping["data_id"] for row in arb_result]

        arb_data = arb_data.alias("arb")

        # now query for Data objects using that above select, adding the
        # "order by max desc" separately
        q = (
            session.query(Data)
            .options(sa.orm.joinedload(Data.foo))
            .select_from(
                datas.join(arb_data, arb_data.c.data_id == datas.c.id)
            )
            .order_by(sa.desc(arb_data.c.max))
            .limit(10)
        )

        # extract "data_id" from the list of result objects
        verify_result = [d.id for d in q]

        eq_(verify_result, arb_result)


class EagerTest4(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "departments",
            metadata,
            Column(
                "department_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("name", String(50)),
        )

        Table(
            "employees",
            metadata,
            Column(
                "person_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("name", String(50)),
            Column(
                "department_id",
                Integer,
                ForeignKey("departments.department_id"),
            ),
        )

    @classmethod
    def setup_classes(cls):
        class Department(cls.Basic):
            pass

        class Employee(cls.Basic):
            pass

    def test_basic(self):
        Department, Employee, employees, departments = (
            self.classes.Department,
            self.classes.Employee,
            self.tables.employees,
            self.tables.departments,
        )

        self.mapper_registry.map_imperatively(Employee, employees)
        self.mapper_registry.map_imperatively(
            Department,
            departments,
            properties=dict(
                employees=relationship(
                    Employee, lazy="joined", backref="department"
                )
            ),
        )

        d1 = Department(name="One")
        for e in "Jim", "Jack", "John", "Susan":
            d1.employees.append(Employee(name=e))

        d2 = Department(name="Two")
        for e in "Joe", "Bob", "Mary", "Wally":
            d2.employees.append(Employee(name=e))

        sess = fixture_session()
        sess.add_all((d1, d2))
        sess.flush()

        q = (
            sess.query(Department)
            .join(Department.employees)
            .filter(Employee.name.startswith("J"))
            .distinct()
            .order_by(sa.desc(Department.name))
        )

        eq_(q.count(), 2)
        assert q[0] is d2


class EagerTest5(fixtures.MappedTest):
    """Construction of AliasedClauses for the same eager load property but
    different parent mappers, due to inheritance."""

    @classmethod
    def define_tables(cls, metadata):
        Table(
            "base",
            metadata,
            Column("uid", String(30), primary_key=True),
            Column("x", String(30)),
        )

        Table(
            "derived",
            metadata,
            Column(
                "uid", String(30), ForeignKey("base.uid"), primary_key=True
            ),
            Column("y", String(30)),
        )

        Table(
            "derivedII",
            metadata,
            Column(
                "uid", String(30), ForeignKey("base.uid"), primary_key=True
            ),
            Column("z", String(30)),
        )

        Table(
            "comments",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("uid", String(30), ForeignKey("base.uid")),
            Column("comment", String(30)),
        )

    @classmethod
    def setup_classes(cls):
        class Base(cls.Basic):
            def __init__(self, uid, x):
                self.uid = uid
                self.x = x

        class Derived(Base):
            def __init__(self, uid, x, y):
                self.uid = uid
                self.x = x
                self.y = y

        class DerivedII(Base):
            def __init__(self, uid, x, z):
                self.uid = uid
                self.x = x
                self.z = z

        class Comment(cls.Basic):
            def __init__(self, uid, comment):
                self.uid = uid
                self.comment = comment

    def test_basic(self):
        (
            Comment,
            Derived,
            derived,
            comments,
            DerivedII,
            Base,
            base,
            derivedII,
        ) = (
            self.classes.Comment,
            self.classes.Derived,
            self.tables.derived,
            self.tables.comments,
            self.classes.DerivedII,
            self.classes.Base,
            self.tables.base,
            self.tables.derivedII,
        )

        self.mapper_registry.map_imperatively(Comment, comments)

        baseMapper = self.mapper_registry.map_imperatively(
            Base,
            base,
            properties=dict(
                comments=relationship(
                    Comment, lazy="joined", cascade="all, delete-orphan"
                )
            ),
        )

        self.mapper_registry.map_imperatively(
            Derived, derived, inherits=baseMapper
        )

        self.mapper_registry.map_imperatively(
            DerivedII, derivedII, inherits=baseMapper
        )

        sess = fixture_session()
        d = Derived("uid1", "x", "y")
        d.comments = [Comment("uid1", "comment")]
        d2 = DerivedII("uid2", "xx", "z")
        d2.comments = [Comment("uid2", "comment")]
        sess.add_all((d, d2))
        sess.flush()
        sess.expunge_all()

        # this eager load sets up an AliasedClauses for the "comment"
        # relationship, then stores it in clauses_by_lead_mapper[mapper for
        # Derived]
        d = sess.get(Derived, "uid1")
        sess.expunge_all()
        assert len([c for c in d.comments]) == 1

        # this eager load sets up an AliasedClauses for the "comment"
        # relationship, and should store it in clauses_by_lead_mapper[mapper
        # for DerivedII].  the bug was that the previous AliasedClause create
        # prevented this population from occurring.
        d2 = sess.get(DerivedII, "uid2")
        sess.expunge_all()

        # object is not in the session; therefore the lazy load can't trigger
        # here, eager load had to succeed
        assert len([c for c in d2.comments]) == 1


class EagerTest6(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "design_types",
            metadata,
            Column(
                "design_type_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
        )

        Table(
            "design",
            metadata,
            Column(
                "design_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column(
                "design_type_id",
                Integer,
                ForeignKey("design_types.design_type_id"),
            ),
        )

        Table(
            "parts",
            metadata,
            Column(
                "part_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("design_id", Integer, ForeignKey("design.design_id")),
            Column(
                "design_type_id",
                Integer,
                ForeignKey("design_types.design_type_id"),
            ),
        )

        Table(
            "inherited_part",
            metadata,
            Column(
                "ip_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("part_id", Integer, ForeignKey("parts.part_id")),
            Column("design_id", Integer, ForeignKey("design.design_id")),
        )

    @classmethod
    def setup_classes(cls):
        class Part(cls.Basic):
            pass

        class Design(cls.Basic):
            pass

        class DesignType(cls.Basic):
            pass

        class InheritedPart(cls.Basic):
            pass

    def test_one(self):
        (
            Part,
            inherited_part,
            design_types,
            DesignType,
            parts,
            design,
            Design,
            InheritedPart,
        ) = (
            self.classes.Part,
            self.tables.inherited_part,
            self.tables.design_types,
            self.classes.DesignType,
            self.tables.parts,
            self.tables.design,
            self.classes.Design,
            self.classes.InheritedPart,
        )

        p_m = self.mapper_registry.map_imperatively(Part, parts)

        self.mapper_registry.map_imperatively(
            InheritedPart,
            inherited_part,
            properties=dict(part=relationship(Part, lazy="joined")),
        )

        d_m = self.mapper_registry.map_imperatively(
            Design,
            design,
            properties=dict(
                inheritedParts=relationship(
                    InheritedPart,
                    cascade="all, delete-orphan",
                    backref="design",
                )
            ),
        )

        self.mapper_registry.map_imperatively(DesignType, design_types)

        d_m.add_property(
            "type", relationship(DesignType, lazy="joined", backref="designs")
        )

        p_m.add_property(
            "design",
            relationship(
                Design,
                lazy="joined",
                backref=backref("parts", cascade="all, delete-orphan"),
            ),
        )

        d = Design()
        sess = fixture_session()
        sess.add(d)
        sess.flush()
        sess.expunge_all()
        x = sess.get(Design, 1)
        x.inheritedParts


class EagerTest7(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "companies",
            metadata,
            Column(
                "company_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("company_name", String(40)),
        )

        Table(
            "addresses",
            metadata,
            Column(
                "address_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("company_id", Integer, ForeignKey("companies.company_id")),
            Column("address", String(40)),
        )

        Table(
            "phone_numbers",
            metadata,
            Column(
                "phone_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("address_id", Integer, ForeignKey("addresses.address_id")),
            Column("type", String(20)),
            Column("number", String(10)),
        )

        Table(
            "invoices",
            metadata,
            Column(
                "invoice_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("company_id", Integer, ForeignKey("companies.company_id")),
            Column("date", sa.DateTime),
        )

    @classmethod
    def setup_classes(cls):
        class Company(cls.Comparable):
            pass

        class Address(cls.Comparable):
            pass

        class Phone(cls.Comparable):
            pass

        class Invoice(cls.Comparable):
            pass

    def test_load_m2o_attached_to_o2(self):
        """
        Tests eager load of a many-to-one attached to a one-to-many.  this
        testcase illustrated the bug, which is that when the single Company is
        loaded, no further processing of the rows occurred in order to load
        the Company's second Address object.

        """

        addresses, invoices, Company, companies, Invoice, Address = (
            self.tables.addresses,
            self.tables.invoices,
            self.classes.Company,
            self.tables.companies,
            self.classes.Invoice,
            self.classes.Address,
        )

        self.mapper_registry.map_imperatively(Address, addresses)

        self.mapper_registry.map_imperatively(
            Company,
            companies,
            properties={"addresses": relationship(Address, lazy="joined")},
        )

        self.mapper_registry.map_imperatively(
            Invoice,
            invoices,
            properties={"company": relationship(Company, lazy="joined")},
        )

        a1 = Address(address="a1 address")
        a2 = Address(address="a2 address")
        c1 = Company(company_name="company 1", addresses=[a1, a2])
        i1 = Invoice(date=datetime.datetime.now(), company=c1)

        session = fixture_session()
        session.add(i1)
        session.flush()

        company_id = c1.company_id
        invoice_id = i1.invoice_id

        session.expunge_all()
        c = session.get(Company, company_id)

        session.expunge_all()
        i = session.get(Invoice, invoice_id)

        def go():
            eq_(c, i.company)
            eq_(c.addresses, i.company.addresses)

        self.assert_sql_count(testing.db, go, 0)


class EagerTest8(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table(
            "prj",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("created", sa.DateTime),
            Column("title", sa.String(100)),
        )

        Table(
            "task",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column(
                "status_id",
                Integer,
                ForeignKey("task_status.id"),
                nullable=False,
            ),
            Column("title", sa.String(100)),
            Column(
                "task_type_id",
                Integer,
                ForeignKey("task_type.id"),
                nullable=False,
            ),
            Column("prj_id", Integer, ForeignKey("prj.id"), nullable=False),
        )

        Table(
            "task_status",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
        )

        Table(
            "task_type",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
        )

        Table(
            "msg",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("posted", sa.DateTime, index=True),
            Column("type_id", Integer, ForeignKey("msg_type.id")),
            Column("task_id", Integer, ForeignKey("task.id")),
        )

        Table(
            "msg_type",
            metadata,
            Column(
                "id", Integer, primary_key=True, test_needs_autoincrement=True
            ),
            Column("name", sa.String(20)),
            Column("display_name", sa.String(20)),
        )

    @classmethod
    def fixtures(cls):
        return dict(
            prj=(("id",), (1,)),
            task_status=(("id",), (1,)),
            task_type=(("id",), (1,)),
            task=(
                ("title", "task_type_id", "status_id", "prj_id"),
                ("task 1", 1, 1, 1),
            ),
        )

    @classmethod
    def setup_classes(cls):
        class Task_Type(cls.Comparable):
            pass

        class Joined(cls.Comparable):
            pass

    def test_nested_joins(self):
        task, Task_Type, Joined, task_type, msg = (
            self.tables.task,
            self.classes.Task_Type,
            self.classes.Joined,
            self.tables.task_type,
            self.tables.msg,
        )

        # this is testing some subtle column resolution stuff,
        # concerning corresponding_column() being extremely accurate
        # as well as how mapper sets up its column properties

        self.mapper_registry.map_imperatively(Task_Type, task_type)

        j = sa.outerjoin(task, msg, task.c.id == msg.c.task_id)
        jj = (
            sa.select(
                task.c.id.label("task_id"),
                sa.func.count(msg.c.id).label("props_cnt"),
            )
            .select_from(j)
            .group_by(task.c.id)
            .alias("prop_c_s")
        )
        jjj = sa.join(task, jj, task.c.id == jj.c.task_id)

        self.mapper_registry.map_imperatively(
            Joined,
            jjj,
            properties=dict(type=relationship(Task_Type, lazy="joined")),
        )

        session = fixture_session()

        eq_(
            session.query(Joined)
            .order_by(Joined.id)
            .limit(10)
            .offset(0)
            .one(),
            Joined(id=1, title="task 1", props_cnt=0),
        )


class EagerTest9(fixtures.MappedTest):
    """Test the usage of query options to eagerly load specific paths.

    This relies upon the 'path' construct used by PropertyOption to relate
    LoaderStrategies to specific paths, as well as the path state maintained
    throughout the query setup/mapper instances process.

    """

    @classmethod
    def define_tables(cls, metadata):
        Table(
            "accounts",
            metadata,
            Column(
                "account_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("name", String(40)),
        )

        Table(
            "transactions",
            metadata,
            Column(
                "transaction_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("name", String(40)),
        )

        Table(
            "entries",
            metadata,
            Column(
                "entry_id",
                Integer,
                primary_key=True,
                test_needs_autoincrement=True,
            ),
            Column("name", String(40)),
            Column("account_id", Integer, ForeignKey("accounts.account_id")),
            Column(
                "transaction_id",
                Integer,
                ForeignKey("transactions.transaction_id"),
            ),
        )

    @classmethod
    def setup_classes(cls):
        class Account(cls.Basic):
            pass

        class Transaction(cls.Basic):
            pass

        class Entry(cls.Basic):
            pass

    @classmethod
    def setup_mappers(cls):
        Account, Transaction, transactions, accounts, entries, Entry = (
            cls.classes.Account,
            cls.classes.Transaction,
            cls.tables.transactions,
            cls.tables.accounts,
            cls.tables.entries,
            cls.classes.Entry,
        )

        cls.mapper_registry.map_imperatively(Account, accounts)

        cls.mapper_registry.map_imperatively(Transaction, transactions)

        cls.mapper_registry.map_imperatively(
            Entry,
            entries,
            properties=dict(
                account=relationship(
                    Account,
                    uselist=False,
                    backref=backref(
                        "entries", lazy="select", order_by=entries.c.entry_id
                    ),
                ),
                transaction=relationship(
                    Transaction,
                    uselist=False,
                    backref=backref(
                        "entries", lazy="joined", order_by=entries.c.entry_id
                    ),
                ),
            ),
        )

    def test_joinedload_on_path(self):
        Entry, Account, Transaction = (
            self.classes.Entry,
            self.classes.Account,
            self.classes.Transaction,
        )

        session = fixture_session()

        tx1 = Transaction(name="tx1")
        tx2 = Transaction(name="tx2")

        acc1 = Account(name="acc1")
        Entry(name="ent11", account=acc1, transaction=tx1)
        Entry(name="ent12", account=acc1, transaction=tx2)

        acc2 = Account(name="acc2")
        Entry(name="ent21", account=acc2, transaction=tx1)
        Entry(name="ent22", account=acc2, transaction=tx2)

        session.add(acc1)
        session.flush()
        session.expunge_all()

        def go():
            # load just the first Account.  eager loading will actually load
            # all objects saved thus far, but will not eagerly load the
            # "accounts" off the immediate "entries"; only the "accounts" off
            # the entries->transaction->entries
            acc = (
                session.query(Account)
                .options(
                    sa.orm.joinedload(Account.entries)
                    .joinedload(Entry.transaction)
                    .joinedload(Transaction.entries)
                    .joinedload(Entry.account)
                )
                .order_by(Account.account_id)
            ).first()

            # no sql occurs
            eq_(acc.name, "acc1")
            eq_(acc.entries[0].transaction.entries[0].account.name, "acc1")
            eq_(acc.entries[0].transaction.entries[1].account.name, "acc2")

            # lazyload triggers but no sql occurs because many-to-one uses
            # cached query.get()
            for e in acc.entries:
                assert e.account is acc

        self.assert_sql_count(testing.db, go, 1)