Handle cases where inserting or deleting unassociated records in counter cache trigger

This was causing the error "The tuple structure of a not-yet-assigned
record is indeterminate".  While here, restructure things in the
counter and sum cache triggers so that UPDATE statements are not sent
for unassociated records.