title: Friend circle by sqlalchemy
body: <p>Say i have the following <code>Users</code> model-</p>
  
  <pre><code>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,
      )
  </code></pre>
  
  <p>And the <code>FriendAssociations</code> model is-</p>
  
  <pre><code>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'
      ),)
  </code></pre>
  
  <p>The target was, user A sends a friend request to user B. Until user B accepts the request, the request stays as pending. When B accepts the request, <code>pending</code> is <code>False</code> and one more <code>friend_associations</code> entry 
  is created on user B to state that user A is friend of user B and vice versa. The problem is, i can do these things, but when i want to remove a user entry, the database(i am using PostgreSQL) throws up error saying <code>friend_associations</code> depends on the user(because the association entry isn't deleted). As a result i can't delete any user entry.</p>
  
  <p>So -</p>
  
  <ol>
  <li>Is my solution to the problem correct?</li>
  <li>If not, what should i do to correct it?</li>
  <li>Please give basic query examples like adding, deleting friends and user entries with such solution or mine.</li>
  </ol>
  
  <p>Thanks in advance. </p>