4

I want to add to existing fields some db_index in my models. I just added db_index=Trueto the model.

I was thinking I would need to run python manage.py migrateto apply them on the database. My issue is that no change is detected thus no migration is created. Is it a normal behavior ?

Or is there a specific behavior I'm not aware of?

Thank you.

2
  • which django version? Commented Dec 3, 2015 at 17:59
  • Django 1.7 right now, planning to make a migration to Django 1.9 soon. Commented Dec 3, 2015 at 18:17

2 Answers 2

1

If you know enough of SQL, you can still create the migration by hand using RunSQL.

For example: (with PostgreSQL syntax)

class Migration(migrations.Migration):

    dependencies = [
        ('your_app', '0001_initial'), # or last mig
    ]

    operations = [
        migrations.RunSQL("CREATE INDEX my_table_my_column_ind ON my_table (my_colum)"),
    ]

Related: Creating Partial Indexes with Django 1.7

Sign up to request clarification or add additional context in comments.

Comments

0

you can use index_together

class YourModel(models.Model):
    # ...

    class Meta:
        index_together = ["field_that_needs_index"]

then makemigration should work

4 Comments

Is there really no way to only add db_index? I have 2 / 3 indexes to create on my models ?
set the db_index in the fields also, but you need migration for it, so add these fields in index_together also.
Ok. how can I check then that my indexes are created ? Creating thos index_together will not cause issues with writes / or database size ?
@AlexGrs nope, no issues. SHOW INDEX FROM [tablename] will show you the indexes from your tables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.