SQLAlchemy:如何将关系定义为其他两个关系的联合?
我如何实现一个自引用的多对多关系,它实际上是两个其他关系的联合 该关系应返回用户与网络中其他用户之间存在的所有FaceBookFriendly模型。用户可能有指向另一个现有用户的FaceBookFriendly,但由于FB API中断、隐私控制等原因,现有用户对该用户的镜像FBFriendly可能不存在SQLAlchemy:如何将关系定义为其他两个关系的联合?,sqlalchemy,python-social-auth,Sqlalchemy,Python Social Auth,我如何实现一个自引用的多对多关系,它实际上是两个其他关系的联合 该关系应返回用户与网络中其他用户之间存在的所有FaceBookFriendly模型。用户可能有指向另一个现有用户的FaceBookFriendly,但由于FB API中断、隐私控制等原因,现有用户对该用户的镜像FBFriendly可能不存在 # This class is necessary for python-social-auth # A UserSocialAuth model only exists for users w
# This class is necessary for python-social-auth
# A UserSocialAuth model only exists for users who are in the network
class UserSocialAuth(_AppSession, Base, SQLAlchemyUserMixin):
"""Social Auth association model"""
__tablename__ = 'social_auth_usersocialauth'
__table_args__ = (UniqueConstraint('provider', 'uid'),)
id = Column(Integer, primary_key=True)
provider = Column(String(32))
uid = Column(String(UID_LENGTH))
extra_data = Column(JSONType())
user_id = Column(
Integer, ForeignKey(User.id), nullable=False, index=True)
user = relationship(
User,
backref=backref('social_auth', lazy='dynamic')
)
other_facebook_friendships = relationship(
FacebookFriendship,
primaryjoin=and_(
uid == FacebookFriendship.fb_uid_friend,
provider == 'facebook'
),
foreign_keys=[provider, uid],
viewonly=True,
uselist=True,
lazy='dynamic',
)
此关系查找从此用户指向任何现有用户的FaceBookFriendly模型
facebook_friendships = relationship(
FacebookFriendship,
primaryjoin=and_(
user_id == FacebookFriendship.user_id,
provider == 'facebook'
),
secondary=FacebookFriendship.__table__,
secondaryjoin=uid == FacebookFriendship.fb_uid_friend,
foreign_keys=[provider, user_id, uid],
viewonly=True,
uselist=True,
lazy='dynamic',
)
此关系查找指向此用户的FaceBookFriendly模型
# This class is necessary for python-social-auth
# A UserSocialAuth model only exists for users who are in the network
class UserSocialAuth(_AppSession, Base, SQLAlchemyUserMixin):
"""Social Auth association model"""
__tablename__ = 'social_auth_usersocialauth'
__table_args__ = (UniqueConstraint('provider', 'uid'),)
id = Column(Integer, primary_key=True)
provider = Column(String(32))
uid = Column(String(UID_LENGTH))
extra_data = Column(JSONType())
user_id = Column(
Integer, ForeignKey(User.id), nullable=False, index=True)
user = relationship(
User,
backref=backref('social_auth', lazy='dynamic')
)
other_facebook_friendships = relationship(
FacebookFriendship,
primaryjoin=and_(
uid == FacebookFriendship.fb_uid_friend,
provider == 'facebook'
),
foreign_keys=[provider, uid],
viewonly=True,
uselist=True,
lazy='dynamic',
)
我能够使用hybrid_属性装饰器来表示联合查询,但这阻止了像any()一样使用比较器或使用关联代理,至少从我所知道的
# Can I rewrite this using relationship()?
@hybrid_property
def all_facebook_friendships(self):
return self.facebook_friendships.union(
self.other_facebook_friendships).correlate(
FacebookFriendship)
# FBFriendship models are created for every friend that a user has,
# regardless of whether they're in the network or not.
class FacebookFriendship(Base):
__tablename__ = u'user_fb_friend'
user_id = Column(Integer, sa.ForeignKey(User.id), primary_key=True)
user = relationship(
User, backref=backref('facebook_friendships', lazy='dynamic'),
primaryjoin=User.id == user_id)
fb_uid_friend = Column(sa.String(length=255), primary_key=True)
最后,我想像查询任何其他InstrumentedAttribute一样查询此关系:
UserSocialAuth.query.filter(UserSocialAuth.all\u facebook\u friendships.any()).all()
并在用户模型上定义关联\u代理:
User.all\u facebook\u friends=association\u proxy('all\u facebook\u friends','User')
对不起,这个问题太长了,但是我已经试了几天了,都没有用了
相关的:
我相信,如果您使用zzzeek的解决方案(使用链接到的
UNION
s),您应该能够完成所有需要的操作,使用any
进行查询也可以。