Backport disable_referential_integrity fix from ActiveRecord

This is basically a backport of rails/rails#8548