bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.46k stars 424 forks source link

DatabaseSchemaIntrospection.get_constraints() can return incorrect values for Django 1.11+ #523

Closed ajmatsick closed 6 years ago

ajmatsick commented 6 years ago

With the addition of class-based indexes in Django 1.11, some changes were made to the way index and constraint data is retrieved and returned in django/db/backends/postgresql/introspection.py.

See:

Of note:

As far as I can tell the omission of the options key doesn't currently have an effect, though it may in the future when using class-based indexes. The incorrect type value, however, can cause errors when changing the db_index and unique values for model fields.

For example, consider a model field that has an index and is then modified to add a unique constraint:

class Migration(migrations.Migration):
    operations = [
        migrations.CreateModel(
            name='Example',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=100, db_index=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),

        migrations.AlterField(
            model_name='Example',
            name='name',
            field=models.CharField(
                max_length=100,
                db_index=True,
                unique=True,
            ),
        )
    ]

This migration will fail with an error similar to this:

django.db.utils.ProgrammingError: relation "app_example_name_f63d8867_like" already exists

This is because the existing index names are not collected correctly (see django/db/backends/base/schema.py), meaning the existing index is not removed before being added again later (here).

To fix this, tenant_schemas/postgresql_backend/introspection.py should be updated to reflect the changes in django/db/backends/postgresql/introspection.py.

ajmatsick commented 6 years ago

Unfortunately I checked the open issues before submitting this, but not the open pull requests. I just saw that #519 addresses the same thing. I can close this if that's going to be merged instead.

g-as commented 6 years ago

whatever gets this fix going!

ajmatsick commented 6 years ago

Fixed by #519