Skip to content

Instantly share code, notes, and snippets.

@mobilestack
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobilestack/8e474d017b57fa62924a to your computer and use it in GitHub Desktop.
Save mobilestack/8e474d017b57fa62924a to your computer and use it in GitHub Desktop.
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

db.create_index

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 'Article.abc'
        db.add_column(u'message_article', 'abc',
                      self.gf('django.db.models.fields.related.ForeignKey')(to=orm['message.HotPoint'], null=True),
                      keep_default=False)


    def backwards(self, orm):
        # Deleting field 'Article.abc'
        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', self.gf('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