Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
south add column of a foreign key contraint type

in south migration, if add a new column of foreign key constraint, only one line is fine. However, if changing from one existing field which is not a foreign key type, i.e. only a integer type, then should add a line of code


after db.alter_column

though create_index is only for speed consideration.

This is for add a new field of fk constraint:

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding field ''
        db.add_column(u'message_article', 'abc',
            'django.db.models.fields.related.ForeignKey')(to=orm['message.HotPoint'], null=True),

    def backwards(self, orm):
        # Deleting field ''
        db.delete_column(u'message_article', 'abc_id')

and this is change from existing interger field to fk:

db.alter_column(u'message_article', 'anon_user_id','django.db.models.fields.related.ForeignKey')(to=orm['users.AnonymousUser'], null=True))
db.create_index(u'message_article', ['anon_user_id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment