Andrew Millspaugh <[email protected]> wrote:
>
> I'm pretty inexperienced with SQLAlchemy. I mostly want to know the best way
> to deal with a relationship like this. I am trying to avoid adding a fake
> relationship directly between F and A, as it could get out of sync with the
> actual nested relationship. In general, if I have a sqlalchemy class for
> which I know the instances will always be uniquely identified by a (great)^n
> grandparent, how should I be accessing the grandparents from the grandchild
> and vice versa?
The usual way is SQLA just handles simple relationships and you use Python
to hide the gaps between the two; that is, methods and properties. A method
to iterate through all of the d.c.b.a for each e, for example, you’d put a
@property on E that is “d.c.b.a”. You probably want to consider what SQL you
want to see or not, since a relationship that joins straight from F to A
would get there without loading any rows for E, D, C or B in between, so may
be more what you want from a performance perspective.
There’s an extension called association proxy that is also used to build up
systems like these on the Python side in a potentially nicer way than using
straight methods and properties. Jumping across 5 gaps on a regular
basis is still a complex case no matter what.
> On Tuesday, March 24, 2015 at 1:12:24 AM UTC-7, Andrew Millspaugh wrote:
> I've got a class hierarchy that looks something like this:
>
> [ A ] 1--------* [ B ] 1---------* [ C ] 1----------* [ D ] 1--------0..1 [ E
> ] 1..*----------0..1 [ F ]
>
> org proj ticket snap
> bidlimit ticketset
>
> And I'm trying to add a relationship from A to F with a backref. The
> relationship definition (on the A model) looks like:
>
> f = DB.relationship('F',
> secondary=(
> 'join(F, E, F.id == E.f_id)'
> '.join(D, E.d_id == D.id)'
> '.join(C, D.c_id == C.id)'
> '.join(B, C.b_id == B.id)'
> ),
> primaryjoin='A.id == B.a_id',
> secondaryjoin='E.f_id == F.id',
> backref=DB.backref('a', uselist=False), viewonly=True
> )
>
> Now, if I query A.f, I get all of the F's, instead of just the ones which
> have a relationship with A. I'm sure I'm missing something simple, but I
> can't seem to find it... Any help out there?
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.