I don't have direct access to Django's repository in the local filesystem. Based on my knowledge of Django's codebase and the issue description, I can generate the fix. The issue is in the migration autodetector where AddIndex operations are generated before AlterOrderWithRespectTo, but AddIndex operations that reference the `_order` field need to depend on AlterOrderWithRespectTo since that's what creates the `_order` field. Looking at the Django migration autodetector, the fix needs to be in the `generate_added_indexes` method to add a dependency on AlterOrderWithRespectTo when an index includes the `_order` field. --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -973,6 +973,14 @@ class MigrationAutodetector: ), ] ) + if index.contains_expressions: + self.add_operation(app_label, operations.AddIndex(model_name=model_name, index=index), dependencies=dependencies) + continue + for field_name in index.fields: + if field_name == '_order': + dependencies.append((app_label, model_name, None, 'order_wrt_unset')) + break + self.add_operation(app_label, operations.AddIndex(model_name=model_name, index=index), dependencies=dependencies) def generate_removed_indexes(self): for (app_label, model_name), alt_indexes in self.altered_indexes.items(): Wait, let me reconsider. The issue is specifically about the ordering of operations. Looking at the PR mentioned in the hints (https://github.com/iurisilvio/django), I need to understand the exact fix location. The `_order` field is created by `AlterOrderWithRespectTo`. When an index includes `_order`, the `AddIndex` operation needs to depend on the `AlterOrderWithRespectTo` operation. In Django's autodetector, the `generate_added_indexes` method creates AddIndex operations. The fix should add a dependency when the index contains the `_order` field. --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -973,6 +973,12 @@ class MigrationAutodetector: ), ] ) + if not index.contains_expressions: + for field_name in index.fields: + if field_name == '_order': + dependencies.append( + (app_label, model_name, None, 'order_wrt_unset'), + ) + break self.add_operation( app_label, operations.AddIndex(model_name=model_name, index=index),