Make sure that when the patron is deleted, the userid (borrowernumber) is either set to NULL or the entries be deleted in search_history.