Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save felixxm/bd5bdf6f8950109dd2db40417e2d3d12 to your computer and use it in GitHub Desktop.
Save felixxm/bd5bdf6f8950109dd2db40417e2d3d12 to your computer and use it in GitHub Desktop.
index_together example
  • 0001_initial.py
class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='MyModel',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('field_1', models.IntegerField()),
                ('field_2', models.TextField()),
                ('field_3', models.IntegerField()),
            ],
            options={
                'index_together': {('field_3',), ('field_1', 'field_2')},
            },
        ),
    ]
  • 0002_alter_mymodel_index_together_mymodel_field_4_and_more.py
class Migration(migrations.Migration):

    dependencies = [
        ('test_one', '0001_initial'),
    ]

    operations = [
        migrations.AlterIndexTogether(
            name='mymodel',
            index_together=set(),
        ),
        migrations.AddField(
            model_name='mymodel',
            name='field_4',
            field=models.IntegerField(null=True),
        ),
        migrations.AlterIndexTogether(
            name='mymodel',
            index_together={('field_2', 'field_4')},
        ),
        migrations.AddIndex(
            model_name='mymodel',
            index=models.Index(fields=['field_1', 'field_2'], name='test_one_my_field_1_ea7372_idx'),
        ),
        migrations.AddIndex(
            model_name='mymodel',
            index=models.Index(fields=['field_3'], name='test_one_my_field_3_d91b6c_idx'),
        ),
        migrations.AddIndex(
            model_name='mymodel',
            index=models.Index(fields=['field_3', 'field_4'], name='test_one_my_field_3_1e8bd9_idx'),
        ),
    ]
  • 0003_remove_mymodel_test_one_my_field_1_ea7372_idx_and_more.py
class Migration(migrations.Migration):

    dependencies = [
        ('test_one', '0002_alter_mymodel_index_together_mymodel_field_4_and_more'),
    ]

    operations = [
        migrations.RemoveIndex(
            model_name='mymodel',
            name='test_one_my_field_1_ea7372_idx',
        ),
        migrations.AlterIndexTogether(
            name='mymodel',
            index_together={('field_2', 'field_4'), ('field_1', 'field_2')},
        ),
    ]
  • 0004_rename_mymodel_field_1_field_2_test_one_my_field_1_ea7372_idx_and_more.py
class Migration(migrations.Migration):

    dependencies = [
        ("test_one", "0003_remove_mymodel_test_one_my_field_1_ea7372_idx_and_more"),
    ]

    operations = [
        migrations.RenameIndex(
            model_name="mymodel",
            new_name="test_one_my_field_1_ea7372_idx",
            old_fields=("field_1", "field_2"),
        ),
        migrations.RenameIndex(
            model_name="mymodel",
            new_name="test_one_my_field_2_26d2ae_idx",
            old_fields=("field_2", "field_4"),
        ),
    ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment