Question

My ecommerce site has a models.py which defines 3 models. Originally, it had only defined two, Product and Category, but I realized I would need to be able to tag my products with certain keywords, and so I added that model. The problem arose when I tried to syncdb and the new tag table was created but NOT the products_tags 'through' table.

class Category(models.Model):
    **a bunch of variables**

    class Meta:
        db_table = 'categories'
        ordering = ['name']
        verbose_name_plural = 'Categories'

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('catalog_category', (), { 'category_slug': self.slug })

class Tag(models.Model):
    **a bunch of variables**

    class Meta:
        db_table = 'tags'
        ordering = ['name']
        verbose_name_plural = 'Tags'

    def __unicode__(self):
        return self.name

class Product(models.Model):
    **a bunch of variables**
    categories = models.ManyToManyField(Category, related_name='cat+')
    tags = models.ManyToManyField(Tag, related_name='tag+')

    class Meta:
        db_table = 'products'
        ordering = ['-created_at']

    def __unicode__(self):
        return self.name

It all validates beautifully, and when I run python manage.py syncdb all the tables are created with the appropriate rows and types and everything else you would expect. Except, it doesn't create the products_tags table you would expect it to create because of the ManyToMany relationship established in the Product class. Why does it successfully create the products_categories 'through' table but not the products_tags one?

Was it helpful?

Solution

As Aamir said in his comment, syncdb has no migration capabilities, and that is why the new through table wasn't being created. I installed django south and now updating tables is going exactly as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top