wiki:db.alter_column

Version 1 (modified by andrew, 5 years ago) (diff)

--

db.alter_column

South 0.4 and up

alter_column(table_name, column_name, field, explicit_name=True)

Alters the column column_name on the table table_name to match field. Note that this cannot alter all field attributes; for example, if you want to make a field unique=True, you should instead use db.add_index? with unique=True, and if you want to make it a primary key, you should look into db.drop_primary_key? and db.create_primary_key?.

If explicit_name is false, ForeignKey? fields will have _id appended to the end of the given column name - this lets you address fields as they are represented in the model itself, rather than as the column name.

Examples

A simple change of the length of a VARCHAR column:

# Assume the table was created with name = models.CharField(max_length=50)
alter_column('core_nation', 'name', models.CharField(max_length=200))

We can also change it to a compatible field type:

alter_column('core_nation', 'name', models.TextField())

If we have a ForeignKey? named 'user', we can address it without the implicit '_id' on the end:

alter_column('core_profile', 'user', models.ForeignKey(orm['auth.User'], null=True, blank=True), explicit_name=False)

Or you can specify the same operation with an explicit name:

alter_column('core_profile', 'user_id', models.ForeignKey(orm['auth.User'], null=True, blank=True))