palewire / django-postgres-copy

Quickly import and export delimited data with Django support for PostgreSQL's COPY command
https://palewi.re/docs/django-postgres-copy/
MIT License
180 stars 48 forks source link

Drop indexes doesn't drop indexes created using Meta.indexes #122

Open mikicz opened 4 years ago

mikicz commented 4 years ago

Since Django 1.11 (current version 3.0), there's a new way of adding indexes to django tables, Meta.indexes (https://docs.djangoproject.com/en/3.0/ref/models/options/#django.db.models.Options.indexes). This package doesn't work with these, it only works with db_index on fields themselves and the Meta.index_together option (which currently has a warning in the docs that Meta.indexes should be used instead).

palewire commented 2 years ago

Can you show me an example that would recreate this bug?

mikicz commented 2 years ago

So imagine you have this table:

from django.db import models
from postgres_copy import CopyManager

class Customer(models.Model):
    first_name = models.CharField(max_length=100, db_index=True)
    last_name = models.CharField(max_length=100, db_index=True)

    objects = CopyManager()

    class Meta:
        indexes = [
            models.Index(fields=['last_name', 'first_name']),
        ]

The indexes regularly look like this (for the two fields there's two indexes, one is for exact and one for %like% (I believe, doesn't really matter I guess). image

If you run Customer.objects.drop_indexes() then the first_name/last_name indexes get dropped and the joined one remains: image