Sort only if id is excluded

Sorting the models if the id is included should be disabled because of foreign key constraints. Another way would be to disable foreign key checks in this case.