title: Sqlalchemy: secondary relationship update
body: <p>I have two tables, say A and B. Both have a primary key id. They have a many-to-many relationship, SEC.</p>
  
  <pre><code>SEC = Table('sec', Base.metadata,
      Column('a_id', Integer, ForeignKey('A.id'), primary_key=True, nullable=False),
      Column('b_id', Integer, ForeignKey('B.id'), primary_key=True, nullable=False)
  )
  
  class A():
     ...
     id = Column(Integer, primary_key=True) 
     ...
     rels = relationship(B, secondary=SEC)
  
  class B():
     ...
     id = Column(Integer, primary_key=True) 
     ...
  </code></pre>
  
  <p>Let's consider this piece of code. </p>
  
  <pre><code>a = A()
  b1 = B()
  b2 = B()
  a.rels = [b1, b2]
  ...
  #some place later
  b3 = B()
  a.rels = [b1, b3]  # errors sometimes
  </code></pre>
  
  <p>Sometimes, I get an error at the last line saying </p>
  
  <pre><code>duplicate key value violates unique constraint a_b_pkey
  </code></pre>
  
  <p>In my understanding, I think it tries to add (a.id, b.id) into 'sec' table again resulting in a unique constraint error. Is that what it is? If so, how can I avoid this? If not, why do I have this error?</p>