title: sqlalchemy python update a primary key with foreign key restraints
body: <p>I need to change a primary key in multiple tables that also have foreign key restraints. I was thinking to either do an update or to copy the entry with an updated primary key and delete the old entry. How can I accomplish either?</p>
  
  <p>Below is a minimal working set up of my tables. P and S rely on the primary key of S and S relies on the primary key of H.</p>
  
  <p>I need to update the a value of ever table for one entry. I am continually limited on changing the value because of the constraints. I would rather not remove the constraints so this can be a permanent solution for later use.</p>
  
  <pre><code>class H(Base):
      __tablename__ = 'ch'
      a = Column(String(255), nullable=False, primary_key=True)
      other_columns = Column(String(255))
  
  class S(Base):
      __tablename__ = 'cs'
      a = Column(String(255), ForeignKey('ch.a'), nullable=False, primary_key=True)
      sn = Column(Integer, nullable=False, primary_key=True)
      other_columns = Column(Date)
      header = relationship("H", backref=backref('cs', order_by=a, cascade='delete, all, delete-orphan'), foreign_keys='S.a')
      __table_args__ = (UniqueConstraint('a', 'sn', name='_a_sn_uc'),)
  
  class P(Base):
      __tablename__ = 'cs_p'
      a = Column(String(255), nullable=False, primary_key=True)
      sn = Column(Integer, nullable=False, primary_key=True)
      ps = Column(Integer, nullable=False, primary_key=True)
      other_columns = Column(String(255))
      pa = relationship("S", backref=backref('cs_p', order_by=a, cascade='delete, all, delete-orphan'))
      __table_args__ = (UniqueConstraint('a', 'sn', 'ps', name='_a_sn_ps_uc'),
                        ForeignKeyConstraint([a, sn], [S.a, S.sn]),)
  
  class F(Base):
      __tablename__ = 'cs_f'
      a = Column(String(255), nullable=False, primary_key=True)
      sn = Column(Integer, nullable=False, primary_key=True)
      fs = Column(Integer, nullable=False, primary_key=True)
      other_columns = Column(String(255))
      fa = relationship("S", backref=backref('cs_f', order_by=a, cascade='delete, all, delete-orphan'))
      __table_args__ = (UniqueConstraint('a', 'sn', 'fs', name='_a_sn_fs_uc'),
                        ForeignKeyConstraint([a, sn], [S.a, S.sn]),)
  </code></pre>