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 -

  1. is solution problem correct?
  2. if not, should correct it?
  3. 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 -

  1. friends - have accepted friend request
  2. sent_friend_requests - sent request user , yet havent accepted
  3. awaiting_friend_requests - have sent request user , yet not accepted

thumbs sqlalchemy documentations.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -