It seems I found the most close solution:
http://stackoverflow.com/a/9119764/383405
I haven’t been aware that secondary can take select() query as well.
Might there be another way to achieve the same by any chance?
On Jun 5, 2013, at 9:27 PM, Hong Minhee <[email protected]> wrote:
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.