Friend circle by sqlalchemy -
say have following users
model-
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, )
and friendassociations
model is-
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' ),)
the target was, user sends friend request user b. until user b accepts request, request stays pending. when b accepts request, pending
false
, 1 more friend_associations
entry created on user b state user friend of user b , vice versa. problem is, can these things, when want remove user entry, database(i using postgresql) throws error saying friend_associations
depends on user(because association entry isn't deleted). result can't delete user entry.
so -
- is solution problem correct?
- if not, should correct it?
- please give basic query examples adding, deleting friends , user entries such solution or mine.
thanks in advance.
ok, found solution reading docs little more. combining cascade, single_parent , passive_deletes achieve 3 relationships -
friends
- have accepted friend requestsent_friend_requests
- sent request user , yet havent acceptedawaiting_friend_requests
- have sent request user , yet not accepted
thumbs sqlalchemy documentations.
Comments
Post a Comment