| 1 | from 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 |
|---|
| 11 | class A(Entity): |
|---|
| 12 | pass |
|---|
| 13 | |
|---|
| 14 | class B(A): |
|---|
| 15 | many_other = OneToMany('Other', inverse='some_a') |
|---|
| 16 | |
|---|
| 17 | class C(A): |
|---|
| 18 | pass |
|---|
| 19 | #many_other = OneToMany('Other') |
|---|
| 20 | |
|---|
| 21 | class Other(Entity): |
|---|
| 22 | some_a = ManyToOne('A') |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | metadata.bind = 'sqlite:///' |
|---|
| 26 | setup_all(True) |
|---|
| 27 | |
|---|
| 28 | a1 = A() |
|---|
| 29 | b1 = B() |
|---|
| 30 | c1 = C() |
|---|
| 31 | o1 = Other(some_a=a1) |
|---|
| 32 | o2 = Other(some_a=b1) |
|---|
| 33 | o3 = Other(some_a=c1) |
|---|
| 34 | |
|---|
| 35 | print "b1", b1.many_other |
|---|
| 36 | print "c1", c1.many_other |
|---|
| 37 | |
|---|
| 38 | session.commit() |
|---|
| 39 | |
|---|
| 40 | print "*" * 20, "A", "*" * 20 |
|---|
| 41 | for 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 |
|---|
| 47 | print "*" * 20, "Other", "*" * 20 |
|---|
| 48 | for o in session.query(Other).all(): |
|---|
| 49 | print o, "->", o.some_a |
|---|