2

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?

fildred13
  • 2,280
  • 8
  • 28
  • 52
  • 1
    Does the product table already exists when you add tags ManyToMany field and you sync db? if yes then django can not identify field level changes in syncdb. Either you need to wipe your db and do sync again or use [django south](http://south.aeracode.org/) – Aamir Rind Mar 28 '13 at 23:50
  • I didn't realize that limitation. I'll have a look at django south. Thanks Aamir! – fildred13 Mar 29 '13 at 00:05

1 Answers1

1

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.

fildred13
  • 2,280
  • 8
  • 28
  • 52