Loading django/db/migrations/operations/models.py +5 −0 Original line number Diff line number Diff line Loading @@ -751,6 +751,11 @@ class AddIndex(Operation): def __init__(self, model_name, index): self.model_name = model_name if not index._name: raise ValueError( "Indexes passed to AddIndex operations require a name " "argument. %r doesn't have one." % index ) self.index = index def state_forwards(self, app_label, state): Loading docs/ref/migration-operations.txt +2 −4 Original line number Diff line number Diff line Loading @@ -215,10 +215,8 @@ For example, to add an index on the ``title`` and ``author`` fields of the ), ] If you're writing your own migration to add an index, it's recommended to pass a ``name`` to the ``index`` as done above so that you can reference it if you later want to remove it. Otherwise, a name will be autogenerated and you'll have to inspect the database to find the index name if you want to remove it. If you're writing your own migration to add an index, you must assign a ``name`` to the ``index`` as done above. ``RemoveIndex`` --------------- Loading tests/migrations/test_operations.py +7 −1 Original line number Diff line number Diff line Loading @@ -1385,7 +1385,13 @@ class OperationTests(OperationTestBase): Test the AddIndex operation. """ project_state = self.set_up_test_model("test_adin") index = models.Index(fields=["pink"]) msg = ( "Indexes passed to AddIndex operations require a name argument. " "<Index: fields='pink'> doesn't have one." ) with self.assertRaisesMessage(ValueError, msg): migrations.AddIndex("Pony", models.Index(fields=["pink"])) index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx") operation = migrations.AddIndex("Pony", index) self.assertEqual(operation.describe(), "Create index on field(s) pink of model Pony") new_state = project_state.clone() Loading Loading
django/db/migrations/operations/models.py +5 −0 Original line number Diff line number Diff line Loading @@ -751,6 +751,11 @@ class AddIndex(Operation): def __init__(self, model_name, index): self.model_name = model_name if not index._name: raise ValueError( "Indexes passed to AddIndex operations require a name " "argument. %r doesn't have one." % index ) self.index = index def state_forwards(self, app_label, state): Loading
docs/ref/migration-operations.txt +2 −4 Original line number Diff line number Diff line Loading @@ -215,10 +215,8 @@ For example, to add an index on the ``title`` and ``author`` fields of the ), ] If you're writing your own migration to add an index, it's recommended to pass a ``name`` to the ``index`` as done above so that you can reference it if you later want to remove it. Otherwise, a name will be autogenerated and you'll have to inspect the database to find the index name if you want to remove it. If you're writing your own migration to add an index, you must assign a ``name`` to the ``index`` as done above. ``RemoveIndex`` --------------- Loading
tests/migrations/test_operations.py +7 −1 Original line number Diff line number Diff line Loading @@ -1385,7 +1385,13 @@ class OperationTests(OperationTestBase): Test the AddIndex operation. """ project_state = self.set_up_test_model("test_adin") index = models.Index(fields=["pink"]) msg = ( "Indexes passed to AddIndex operations require a name argument. " "<Index: fields='pink'> doesn't have one." ) with self.assertRaisesMessage(ValueError, msg): migrations.AddIndex("Pony", models.Index(fields=["pink"])) index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx") operation = migrations.AddIndex("Pony", index) self.assertEqual(operation.describe(), "Create index on field(s) pink of model Pony") new_state = project_state.clone() Loading