Hi,

I have two tables User and Following, and User has two relationships followings 
and followers.  It’s so typical, and next I want is readonly relationship to 
union of followings and followers.  I tried like:

    friends = relationship(
        'User',
        collection_class=set,
        primaryjoin='''or_(
            and_(foreign(User.id) == Following.follower_id,
                 remote(User.id) == Following.followee_id),
            and_(foreign(User.id) == Following.followee_id,
                 remote(User.id) == Following.follower_id)
        )''',
        viewonly=True
    )

but it seems to no work because foreign_keys and remote_side are the same 
columns (User.id).  The error message is:

    sqlalchemy.exc.ArgumentError: Can't determine relationship direction for 
relationship 'User.friends' - foreign key columns within the join condition are 
present in both the parent and the child's mapped tables.  Ensure that only 
those columns referring to a parent column are marked as foreign, either via 
the foreign() annotation or via the foreign_keys argument.

Basically I want to find how to specify foreign_keys and remote_side in such 
self-referential many-to-many relationships, but it’s okay if there’re any 
other ways to implement the same thing.  I just need to use such relationship 
in query expressions e.g. query.filter(User.friends.any(…)), 
query.options(joinedload(User.friends)) and instance properties e.g. 
map(make_json, user.friends).  (If I implement it using @property decorator it 
won’t work with query expressions.)

I read these two chapters in the docs:

    
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#self-referential-many-to-many-relationship
    
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#creating-custom-foreign-conditions

Are there any other resources to get more hints about it?

Thanks,
Hong Minhee

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to