title: Im making a Many to Many relationship in SQLAlchemy. How do I solve my intergrity error?
body: <p>I have 2 classes and I am making an association table via the relationship method.</p>
  
  <pre><code>class channel(Base):
      __tablename__ = 'channel'
  
      id = Column(Integer, primary_key=True, autoincrement=False)
      attribute2 = Column(Integer)
  
      def __init__(self,channel):
          self.id    = channel.get('id', None)
          self.attribute2 = channel.get('attribute2', None)
  
  class subscriptionpack(Base):
      __tablename__ = 'subscriptionpack'
  
      id = Column(Integer, primary_key=True, autoincrement=False)
      title = Column(String(45))
  
      channels = relationship('channel', secondary=subscriptionpack_channel, backref='subscription_packs', lazy = 'dynamic')
  
      def __init__(self,subscriptionpack):
          self.id = subscriptionpack['id']
          self.title = subscriptionpack['title'] 
  
  subscriptionpack_channel = Table('subscriptionpack_channel', Base.metadata,
          Column('channel_id', Integer, ForeignKey("channel.id")),
          Column('ss_pack_id', Integer, ForeignKey("subscriptionpack.id"))
      )
  
     def example_main():
          session = sessionmaker(bind=self.db)
  
          for ch in channel_list:
              ch_obj = channel(ch)
              for ss_pack in ch['subscriptionpack']:
                  ch_obj.subscription_packs.append(ss_pack)
  
              session.add(ch_obj)
          session.commit()
          session.close()
  </code></pre>
  
  <p>I get :</p>
  
  <blockquote>
    <p>sqlalchemy.exc.IntegrityError: (_mysql_exceptions.IntegrityError)
    (1062, "Duplicate entry '161' for key 'PRIMARY'") [SQL: u'INSERT INTO
    subscriptionpack</p>
  </blockquote>
  
  <p>I have been successful using <code>session.merge(ch_obj)</code> but I do not want to update database if primary key is already in there.</p>
  
  <p>Channels have many subscriptions.<br>
  I want to add entry into the association table via the channel object without automatically updating the <code>subscriptionpack</code> table. Is that possible? Would that be the right approach?</p>
  
  <pre><code>Example:
  1. channel1 has subscriptions 1 and 2
  2. channel2 has subscriptions 1 and 2
  3. channel3 has subscriptions 1 and 3
  </code></pre>