It's more of a convenience thing. I want a nice way to be able to do 
something like:

A.fs
or
F.a

For example, if a user is only allowed to modify an F object if they have a 
key for the corresponding a object, I'd like to write something like:
if current_user.authorized(F.a):
    # do something

On the other side, I'd like to be able to get the list of all fs on an A 
without having to do a query. I want to be able to write something like:
for f_instance in A.fs:
    # do something

If you think the best way to do this is to simply switch to using queries, 
I'm fine with that. I just felt that it was awkward. It will change my 
procedures to something like:

if current_user.authorized(F.query.join(*F.join_to_a()).one()):
    # do something

for f_instance in F.query.join(F.a).all():
    # do something

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?


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.

Reply via email to