Opened 7 years ago
Last modified 7 years ago
#28888 new Cleanup/optimization
Index added to _meta.indexes with Meta.indexes=[] yields two equal addIndex() operations. — at Initial Version
Description ¶
I use a custom field derived from django.contrib.postgres.fields.JSONField
, which adds an GinIndex
to model._meta.indexes
:
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.indexes import GinIndex class CustomJSONField(JSONField): def contribute_to_class(self, cls, name): super(CustomJSONField, self).contribute_to_class(cls, name) index = GinIndex(fields=[name]) index.set_name_with_model(cls) cls._meta.indexes.append(index)
When used in a model like this,
class Blog(models.Model): title = models.CharField(max_length=100) json = CustomJSONField()
Migrations for model and index are created as expected:
./manage.py --version 1.11.8 ./manage.py makemigrations app Migrations for 'app': app/migrations/0001_initial.py - Create model Blog - Create index app_blog_json_2cf556_gin on field(s) json of model blog
But when I add an empty list of indexes like this:
class Blog(models.Model): title = models.CharField(max_length=100) json = CustomJSONField() class Meta: indexes = []
two indexes are created:
rm -rf app/migrations ./manage.py --version 1.11.8 ./manage.py makemigrations app Migrations for 'app': app/migrations/0001_initial.py - Create model Blog - Create index app_blog_json_2cf556_gin on field(s) json of model blog - Create index app_blog_json_2cf556_gin on field(s) json of model blog
Which of course results in django.db.utils.ProgrammingError: relation "app_blog_json_2cf556_gin" already exists
.
According to the ticket's flags, the next step(s) to move this issue forward are:
Unknown. The Someday/Maybe triage stage is used to keep track of high-level ideas or long term feature requests.
It could be an issue that's blocked until a future version of Django (if so, Keywords will contain that version number). It could also be an enhancement request that we might consider adding someday to the framework if an excellent patch is submitted.
If you're interested in contributing to the issue, raising your ideas on the Django Forum would be a great place to start.