def _update_due(self, team, id, cursor=None):
        # Copy due to new record
        (cursor or self.db).run("""
            UPDATE payment_instructions p
               SET due = COALESCE((
                      SELECT due
                        FROM payment_instructions s
                       WHERE participant=%(username)s
                         AND team = %(team)s
                         AND due > 0
                   ), 0)
             WHERE p.id = %(id)s
        """, dict(username=self.username,team=team,id=id))

        # Reset older due values to 0
        self._reset_due(team, except_for=id, cursor=cursor)
        (cursor or self.db).run("""
            UPDATE payment_instructions p
               SET due = 0
             WHERE participant = %(username)s
               AND team = %(team)s
               AND due > 0
               AND p.id != %(id)s
        """, dict(username=self.username,team=team,id=id))
Transfer existing due value to newly inserted record
dataset/ETHPy150Open gratipay/gratipay.com/gratipay/models/participant.py/Participant._update_due