I'm adding a NULL column to a large table, and I'd like to make sure that it has db_index=True set in the model definition:
class MyLargeModel(Model):
new_field = IntegerField(null=True, default=None, db_index=True)
But for the actual index I'd like to use a partial index:
migrations.RunSQL("""
CREATE INDEX my_index
ON mylargemodel (new_field)
WHERE new_field IS NOT NULL
""")
However, by default, this will mean Django's migrations will create an index (which will be very slow), then I'll need to manually drop that index and create my own.
How can I create a migration which will tell Django Migrations that db_index=True is set without creating the index in the database?
db_index=Truewhen you create your own index?db_index=Trueso that future developers (and future me) will know it's indexed and won't need to dig around to wonder what's going on. I know I could add a comment… but I'd rather have the explicit attribute.