Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#26889 closed Bug (fixed)

Index creation different if SlugField in CreateModel or in AddField of a migration on postgresql

Reported by: aRkadeFR Owned by: nobody
Component: Migrations Version: 1.9
Severity: Normal Keywords: migrations, index, postgresql, slugfield db-indexes
Cc: jon.dufresne@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

I've run into a different index creation behavior of the django migrate command if a SlugField of a Model is created in the CreateModel method in a migration or in a AddField method.

If we have a model like so:

from django.db import models


class TestModel(models.Model):
    """ DocString for the class """
    value = models.IntegerField('value of the model')
    slug_model = models.SlugField()

If we leave the slug_models creation in the CreateModel method of the migration, an index testt_models_testmodel_slug_model_f9e19be9_like is created

# the index
"testt_models_testmodel_slug_model_f9e19be9_like" btree (slug_model varchar_pattern_ops)

# the migration
migrations.CreateModel(
    name='TestModel',
    fields=[
        ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ('value', models.IntegerField(verbose_name='value of the model')),
        ('slug_model', models.SlugField()),
    ],
),

Now if we create the slug_field with a CreateField method in the migrations, we don't have the index (that happen if it's actually a migration after the 0001_initial of an app).

# migration
migrations.CreateModel(
    name='TestModel',
    fields=[
        ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ('value', models.IntegerField(verbose_name='value of the model')),
    ],
),
migrations.AddField(
    model_name='testmodel',
    name='slug_model',
    field=models.SlugField(),
),

I would expect either django-migrations to not create index, or have consistent schema output between the two methods.

Thanks

Change History (9)

comment:1 Changed 3 years ago by Tim Graham

Keywords: db-indexes added
Triage Stage: UnreviewedAccepted

This looks like the same issue as #25412 but for AddField instead of AlterField.

comment:2 Changed 3 years ago by Jon Dufresne

Cc: jon.dufresne@… added
Has patch: set

comment:3 Changed 3 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:4 Changed 3 years ago by Jon Dufresne <jon.dufresne@…>

Resolution: fixed
Status: newclosed

In 2e4cfcd2:

Fixed #26889 -- Fixed missing PostgreSQL index in SchemaEditor.add_field().

comment:5 Changed 3 years ago by Tim Graham <timograham@…>

In b6af8b6:

[1.9.x] Fixed #26889 -- Fixed missing PostgreSQL index in SchemaEditor.add_field().

Backport of 2e4cfcd2b9a0984ad6c4087a5deebbf33413835c from master

comment:6 Changed 3 years ago by Tim Graham <timograham@…>

In 1a2ee697:

[1.10.x] Fixed #26889 -- Fixed missing PostgreSQL index in SchemaEditor.add_field().

Backport of 2e4cfcd2b9a0984ad6c4087a5deebbf33413835c from master

comment:7 Changed 3 years ago by Tim Graham <timograham@…>

In 8edfdddb:

[1.8.x] Fixed #26889 -- Fixed missing PostgreSQL index in SchemaEditor.add_field().

Backport of 2e4cfcd2b9a0984ad6c4087a5deebbf33413835c from master

comment:8 Changed 3 years ago by Tim Graham <timograham@…>

In 3f76d140:

Refs #26889 -- Refactored SchemaEditor to allow backend specific indexes.

comment:9 Changed 3 years ago by aRkadeFR

Thanks for the work done :)

Note: See TracTickets for help on using tickets.
Back to Top