Search code examples
djangomigrationmany-to-manydjango-south

South ignores ManyToManyField in my model


My model:

class Article(models.Model):
    user    = models.ForeignKey(User)
    categories = models.ManyToManyField(AuditGroup)
    topic   = models.ManyToManyField(Topic)
    title   = models.CharField(max_length=255)
    short_desc = models.TextField(blank=True)

The migration created:

class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding model 'Article'
        db.create_table('certification_article', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
            ('title', self.gf('django.db.models.fields.CharField')(max_length=255)),
            ('short_desc', self.gf('django.db.models.fields.TextField')(blank=True)),
        ))

None of the two many-to-many relationships are being produced! What am I missing?

Note: there's a strange thing in my model (took over the project): There is a class ProgramOverview in my model.py. But all code in this class is lowercase! In fact running --auto on schemamigration produces errors about ProgramOverview. Removing it, south wants to delete this class (turns out this is a view in the DB which is needed!) --> This seems to have been put there for some "hacky-ish" reason...So I produced the migration with:

./manage.py schemamigration certification --add-model Article

EDIT: This is the real problem. Somehow my editor messed up the ProgramOverview code. After restoring the code, I was able to run ./manage.py schemamigration certification --auto which produced all the needed tables! END EDIT

I need the many-to-many though.


Solution

  • You wouldn't see an M2M in that declaration. What you see is correct.

    An M2M "field" is an abstraction for a new table. There is no database level field in the 'Article' model.

    Scan down the page to see the relevant M2M table creation code.