fix bug with querying for linked records and deleting records