2

I want to understand if adding or removing indexes from models creates issues with migrations

Please indicate if any step(s) cause issues with migrations

A) On Day One I create the following model

class Person(models.Model):
    person_name = models.CharField(max_length=50)
    create_date = models.DateTimeField()
    country = models.ForeignKey(Country, on_delete=models.CASCADE)

run migrate and makemigrations commands

B) 20,000 records of data are added for the Person model

C) I add an index to the person_name field

class Person(models.Model):
    person_name = models.CharField(max_length=50, db_index=True)
    create_date = models.DateTimeField()
    country = models.ForeignKey(Country, on_delete=models.CASCADE)

run migrate and makemigrations commands

D) I remove the index from the person_name field

class Person(models.Model):
    person_name = models.CharField(max_length=50)
    create_date = models.DateTimeField()
    country = models.ForeignKey(Country, on_delete=models.CASCADE)

run migrate and makemigrations commands

Padoga
  • 495
  • 3
  • 18
  • Why do you think they would cause issues? If you worry about migrations you can always create a db copy (or just populate with test data) and execute migrations on it before applying them to production db – awesoon Oct 27 '20 at 06:43
  • I thought it might cause issues because I read https://stackoverflow.com/questions/41496690/django-adding-db-index-to-existing-table about not being able to add `db_index=True` to a table with data, but I think it is incorrect and is fine to add `db_index=True` later. – Padoga Oct 27 '20 at 07:03
  • 2
    Yes, it is perfectly fine to add or remove indices (I agree with this [comment](https://stackoverflow.com/questions/41496690/django-adding-db-index-to-existing-table#comment83758002_41499278) that author is probably tried to add an index to fk). There can be issues if you have a lot of data (index creation may be slow, queries may become slow after removing an index), but they are mostly related to db and queries, not django. – awesoon Oct 27 '20 at 07:51

0 Answers0