Ticket #78: test_polymorphic_rel.py

File test_polymorphic_rel.py, 1.1 kB (added by ged, 5 years ago)
Line 
1from elixir import *
2
3# TODO: this usecase (explicit "inverse" on child entity) is buggy: it adds a
4# backref on some_a, which means, the relation appears on A and C objects in
5# addition to B !!!
6# solution: 1) do not allow such inverse to be specified (and change exception
7#              message)
8#       and 2) allow the OneToMany relationship to exist anyway, if the user
9#              passes manual primary_join (involving the col created by the
10#              M2O) OR backref=False
11class A(Entity):
12    pass
13
14class B(A):
15    many_other = OneToMany('Other', inverse='some_a')
16
17class C(A):
18    pass
19    #many_other = OneToMany('Other')
20
21class Other(Entity):
22    some_a = ManyToOne('A')
23
24
25metadata.bind = 'sqlite:///'
26setup_all(True)
27
28a1 = A()
29b1 = B()
30c1 = C()
31o1 = Other(some_a=a1)
32o2 = Other(some_a=b1)
33o3 = Other(some_a=c1)
34
35print "b1", b1.many_other
36print "c1", c1.many_other
37
38session.commit()
39
40print "*" * 20, "A", "*" * 20
41for a in session.query(A).all():
42    print "a", a,
43    if hasattr(a, 'many_other'):
44        print "->", a.many_other
45    else:
46        print
47print "*" * 20, "Other", "*" * 20
48for o in session.query(Other).all():
49    print o, "->", o.some_a