Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To build on <a href="https://stackoverflow.com/users/215414/potr-czachur">Potr Czachur</a>'s <a href="https://stackoverflow.com/a/1770138/305736">answer</a>, situations that involve ForeignKeys are more complicated and should be handled slightly differently.</p> <p>(The following example builds on the <code>common</code> and <code>specific</code> apps referred to the in the current answer).</p> <pre><code># common/models.py class Cat(models.Model): # ... class Toy(models.Model): belongs_to = models.ForeignKey(Cat) # ... </code></pre> <p>would then change to</p> <pre><code># common/models.py from specific.models import Cat class Toy(models.Model): belongs_to = models.ForeignKey(Cat) # ... # specific/models.py class Cat(models.Model): # ... </code></pre> <p>Running</p> <pre><code>./manage.py schemamigration common --auto ./manage.py schemamigration specific --auto # or --initial </code></pre> <p>would generate the following the migrations (I'm intentionally ignoring Django ContentType changes—see previously referenced answer for how to handle that):</p> <pre><code># common/migrations/0009_auto__del_cat.py class Migration(SchemaMigration): def forwards(self, orm): db.delete_table('common_cat') db.alter_column('common_toy', 'belongs_to_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['specific.Cat'])) def backwards(self, orm): db.create_table('common_cat', ( # ... )) db.alter_column('common_toy', 'belongs_to_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['common.Cat'])) # specific/migrations/0004_auto__add_cat.py class Migration(SchemaMigration): def forwards(self, orm): db.create_table('specific_cat', ( # ... )) def backwards(self, orm): db.delete_table('specific_cat') </code></pre> <p>As you can see, the FK must be altered to reference the new table. We need to add a dependency so that we know the order in which the migrations will be applied (and thus that the table will exist before we try to add a FK to it) but we also need to make sure rolling backwards works too because <strong>the dependency applies in the reverse direction</strong>.</p> <pre><code># common/migrations/0009_auto__del_cat.py class Migration(SchemaMigration): depends_on = ( ('specific', '0004_auto__add_cat'), ) def forwards(self, orm): db.alter_column('common_toy', 'belongs_to_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['specific.Cat'])) def backwards(self, orm): db.rename_table('specific_cat', 'common_cat') db.alter_column('common_toy', 'belongs_to_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['common.Cat'])) # specific/migrations/0004_auto__add_cat.py class Migration(SchemaMigration): def forwards(self, orm): db.rename_table('common_cat', 'specific_cat') def backwards(self, orm): pass </code></pre> <p>Per the <a href="http://south.readthedocs.org/en/latest/dependencies.html" rel="nofollow noreferrer">South documentation</a>, <code>depends_on</code> will ensure that <code>0004_auto__add_cat</code> runs before <code>0009_auto__del_cat</code> <strong>when migrating forwards</strong> but in the <strong>opposite order when migrating backwards</strong>. If we left <code>db.rename_table('specific_cat', 'common_cat')</code> in the <code>specific</code> rollback, the <code>common</code> rollback would fail when trying to migrate the ForeignKey because the table referenced table wouldn't exist.</p> <p>Hopefully this is closer to a "real world" situation than the existing solutions and someone will find this helpful. Cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload