Modify ↓
Ticket #376 (closed defect: fixed)
addding db_index not executed
| Reported by: | anonymous | Owned by: | andrew |
|---|---|---|---|
| Priority: | major | Milestone: | 0.7 |
| Component: | databaseapi | Version: | 0.7-pre |
| Keywords: | Cc: |
Description
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$ ./manage.py 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$ ./manage.py 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.
results
The database did _not_ get the index
./manage.py 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)
version
changeset: 695:8ee701cb2279 tag: tip user: Andrew Godwin <andrew@aeracode.org> date: Wed Mar 03 19:45:10 2010 +0000 summary: Fix #368: Use keep_default=False
DB: mysql
details
The generated (& failing) code is
def forwards(self, orm):
# Changing field 'Knight.dances_whenever_able'
db.alter_column('southtut_knight', 'dances_whenever_able', self.gf('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', self.gf('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'])
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

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].