Retrieve and script foreign keys with deterministic order