Ticket #376 (closed defect: fixed)

Opened 4 years ago

Last modified 4 years ago

addding db_index not executed

Reported by: anonymous Owned by: andrew
Priority: major Milestone: 0.7
Component: databaseapi Version: 0.7-pre
Keywords: Cc:


Adding an db_index=True fails to actually create an index in the database.

When modifying a model, by adding a db_index=True, south detects this and generates code to migrate this.
The migration however has no effect in the database (mysql)

is there a bug in db.alter_column ?

steps to reproduce

Take an existing model,

class Knight(models.Model):
   name = models.CharField(max_length=100,unique=True)
   of_the_round_table = models.BooleanField()                                                                                         dances_whenever_able = models.BooleanField()

import it in south.

Then add an index

class Knight(models.Model):
   name = models.CharField(max_length=100,unique=True)
   of_the_round_table = models.BooleanField()
   dances_whenever_able = models.BooleanField(db_index=True)

create migration

(python2.5)harm@dev2:~/svn/idq/trunk/projects/idqsso/django/projects/southtutorial$ ./ schemamigration southtut --auto
 ~ Changed field dances_whenever_able on southtut.Knight

and migrate

(python2.5)harm@dev2:~/svn/idq/trunk/projects/idqsso/django/projects/southtutorial$ ./ migrate southtut
Running migrations for southtut:
 - Migrating forwards to 0002_auto__chg_field_knight_dances_whenever_able.
 > southtut:0002_auto__chg_field_knight_dances_whenever_able
 - Loading initial data for southtut.


The database did _not_ get the index

 ./ dbshell
mysql> show index from southtut_knight;
| Table           | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
| southtut_knight |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| southtut_knight |          0 | name     |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |
2 rows in set (0.00 sec)


changeset:   695:8ee701cb2279
tag:         tip
user:        Andrew Godwin <>
date:        Wed Mar 03 19:45:10 2010 +0000
summary:     Fix #368: Use keep_default=False

DB: mysql


The generated (& failing) code is

  def forwards(self, orm):
        # Changing field 'Knight.dances_whenever_able'
        db.alter_column('southtut_knight', 'dances_whenever_able','django.db.models.fields.BooleanField')(db_index=True, blank=True))

    def backwards(self, orm):

    # Changing field 'Knight.dances_whenever_able'
        db.alter_column('southtut_knight', 'dances_whenever_able','django.db.models.fields.BooleanField')(blank=True))

when manually fixing the migration class to the following then it _does_ work. This makes me think there is a bug in db.alter_column

# manually edited code
 def forwards(self, orm):
        db.create_index('southtut_knight', ['dances_whenever_able'], unique=False)

    def backwards(self, orm):
        db.delete_index('southtut_knight', ['dances_whenever_able'])


Change History

comment:1 Changed 4 years ago by andrew

  • Status changed from new to closed
  • Resolution set to fixed
  • Milestone set to 0.7

An excellent bug report, with one small issue; in the example, you've added the column, not altered it, and db_index was only failing on altering!

Nevertheless, I've fixed the issue with altering in [be0c2958f2c2].


Add a comment

Modify Ticket

as closed
The resolution will be deleted. Next status will be 'reopened'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.