# \`makemigrations\` ignores the index added to an existing model

**URL:** <https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495>\
**Category:** Using the ORM\
**Created:** [March 7, 2022, 11:35pm UTC](https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495 "2022-03-07T23:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simpleopen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simpleopen/32/2778_2.png) [@simpleopen](https://forum.djangoproject.com/u/simpleopen)\
**Post date:** [March 7, 2022, 11:35pm UTC](https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495/1 "2022-03-07T23:35:36Z")

</div>

In a project with Django 3.1.4, I am trying to add an index to an existing column in the database, so I modified the `models.py` file and added `db_index=True` to the field’s arguments.

However, calling the command `python manage.py makemigrations app` does not detect the additional indexing.

For testing, I made other changes to the model, e.g., adding a new field, and the command detected it correctly.

I am new to Django migrations and will appreciate any hints or suggestions. Also, please let me know if you need more information.

**Technical Details:**

1. The source code before the change:

```auto
...
    entity_ct = models.ForeignKey(ContentType, related_name='value_entities',
                                  on_delete=models.DO_NOTHING)
...

```

1. And after the change:

```auto
...
    entity_ct = models.ForeignKey(ContentType, related_name='value_entities',
                                  on_delete=models.DO_NOTHING, db_index=True)
...

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 7, 2022, 11:58pm UTC](https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495/2 "2022-03-07T23:58:27Z")

</div>

I wouldn’t expect any changes to be made. A ForeignKey field automatically creates an index. If you’re using PostgreSQL, you can examine the schema and see that the indexes exist.

See the last paragraph in the section [ForeignKey](https://docs.djangoproject.com/en/4.0/ref/models/fields/#foreignkey) in the docs.

---

<div class="post-metadata">

**Author:** ![simpleopen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/simpleopen/32/2778_2.png) [@simpleopen](https://forum.djangoproject.com/u/simpleopen)\
**Post date:** [March 8, 2022, 12:32am UTC](https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495/3 "2022-03-08T00:32:19Z")

</div>

Thank you, @KenWhitesell. My project is running MariaDB InnoDB, and the system behaviour is the same as you say.

It looks like there is no explicit instruction to create the index in the model definition, the migration, or its SQL translation. So, I think the database automatically creates an index for a foreign key by default.

I also found the below message on [MySQL’s official website](https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html):

> - MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the _first_ columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if you create another index that can be used to enforce the foreign key constraint. _`index_name`_ , if given, is used as described previously.

---

<div class="post-metadata">

**Author:** ![badeendjuh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/badeendjuh/32/12026_2.png) [@badeendjuh](https://forum.djangoproject.com/u/badeendjuh)\
**Post date:** [February 3, 2023, 10:55am UTC](https://forum.djangoproject.com/t/makemigrations-ignores-the-index-added-to-an-existing-model/12495/4 "2023-02-03T10:55:55Z")

</div>

Not all databases automatically create indexes for FK fields

For example Oracle [does not do so](https://stackoverflow.com/questions/2154091/in-which-cases-will-oracle-create-indexes-automatically,) unless there is a unique constraint. It offers other features instead, such as [not having to think about indexes at all](https://www.oracle.com/news/connect/oracle-database-automatic-indexing.html)

In run into a problem with this and had to manually create the index as db\_index=True is ignored by django.
