Added foreign keys, aggregations and corrected sync order
Simplified reversing code
Generalized foreign key field quotes
Extracted foreign key
Foreign key vs primary key check