Adding a “through” table to django field and migrating with South?

后端 未结 3 1907
-上瘾入骨i
-上瘾入骨i 2021-01-04 01:41

Seems like this should be \"easy\" or at least documented somewhere, I just cant find it.

Lets say I have a model:

class A(models.Model):
    users =         


        
相关标签:
3条回答
  • 2021-01-04 01:54

    As mentioned in a comment, the first step may be simplified using db.rename_table as described here, which gives this through model:

    class AUsers(models.Model):
    user = models.ForeignKey('auth.User')
    a = models.ForeignKey('A')
    
    class Meta:
        unique_together = (('user', 'a'),)
    

    Then, create a migration with --auto (this way you'll have the names of the DB tables visible), and replace the content with:

    class Migration(SchemaMigration):
    
        def forwards(self, orm):
            db.rename_table('appname_a_user', 'appname_auser')
    
        def backwards(self, orm):
            db.rename_table('appname_auser','appname_a_user') 
    

    I just applied it in my project without issues.

    0 讨论(0)
  • 2021-01-04 02:06

    If anyone comes across this question when trying to do the same thing with the moderns migration framework, here are the steps:

    1. Create a new model class that exactly matches the built-in through table
    2. Use the Meta class to set the table name to match the existing table
    3. Generate a migration, which will create the new table and set it as the through for the field.
    4. Without running that migration, edit it to wrap it in a migrations. SeparateDatabaseAndState migration, where the auto-generated steps are in the state_operations field and the database operations are empty.
    5. Modify your through table, as required, making sure to generate new migrations as normal.
    0 讨论(0)
  • 2021-01-04 02:11

    You should be able to do this pretty easily.

    First of all, make sure that the manual through table that you are creating has the same table name in the database as the one Django originally created automatically.

    So, first, let's consider a manual through model before your change:

    class AUsers(models.Model):
        user = models.ForeignKey('auth.User')
        a = models.ForeignKey('A')
    
        class Meta:
            db_table = 'appname_a_user'
    

    That should be functionally (almost) identical to the ManyToManyField you used to have. Actually, you could make an empty migration and apply it, and then use --auto for your changes (but don't).

    Now, add your field like you did in your sample code above, and then run ./manage.py schemamigration appname manual_through_table --empty. That will give you an empty migration named ####_manual_through_table.py.

    In the migration itself, there will be a forwards and backwards method. Each one needs to be one line each:

    def forwards(self, orm):
        db.add_column('appname_a_user', 'new_field', self.gf('django.db.models.fields.BooleanField')(default=False))
    
    def backwards(self, orm):
        db.delete_column('appname_a_user', 'new_field')
    

    That should get you what you are after.

    0 讨论(0)
提交回复
热议问题