Fixed indexing bug in delete_record and tidied up sql logging.
Simplified query params.
Fixed loading of foreign keys.