说我有以下Users
型号 -
class Users(Base):
__tablename__='users'
id=Column(Integer, primary=True)
friends=relationship(
'Users',
secondary='friend_associations',
primaryjoin='and_(FriendAssociations.user_id==Users.id,'
'FriendAssociations.pending==False)',
secondaryjoin='and_(FriendAssociations.friend_id==Users.id,'
'FriendAssociations.pending==False)',
uselist=True,
)
FriendAssociations
模型是 -
class FriendAssociations(Base):
__tablename__='friend_associations'
id=Column(Integer, primary=True)
user_id=Column(ForeignKey('Users.id'), nullable=False)
friend_id=Column(ForeignKey('Users.id'), nullable=False)
pending=Column(Boolean, default=True)
__table_args__ = (UniqueConstraint(
'user_id','friend_id', name='uq_user_friend_id_pair'
),)
目标是,用户A向用户B发送朋友请求。在用户B接受请求之前,请求保持为待处理状态。当B接受请求时,pending
为False
,还有一个friend_associations
条目
在用户B上创建以声明用户A是用户B的朋友,反之亦然。问题是,我可以做这些事情,但是当我想删除用户条目时,数据库(我正在使用PostgreSQL)会引发错误,说friend_associations
取决于用户(因为关联条目没有被删除) )。因此,我无法删除任何用户条目。
所以 -
提前致谢。
答案 0 :(得分:0)
好的,我找到了解决文档的解决方案。结合cascade,single_parent和passive_deletes我可以实现3种关系 -
friends
- 已接受好友请求的人sent_friend_requests
- 向用户发送请求但尚未接受awaiting_friend_requests
- 已向用户发送请求但尚未接受的人赞成SQLAlchemy文档。