Changes to improve ordering of constraints to avoid foreign key issues

And other things along those lines.