0

I have this model

class Category(models.Model):
category_name= models.CharField(max_length=255)
sub_category_ID = models.IntegerField(null=True, blank=True)
def __unicode__(self):
    return self.category_name

I already have data in the table but I want to change the sub_category_ID to without deleting the entire database.

class Category(models.Model):
category_name= models.CharField(max_length=255)
sub_category_ID = models.ForeignKey('self',null=True, blank=True)
def __unicode__(self):
    return self.category_name

So I run syncdb after I changed the model and it gave me the warning.

The following content types are stale and need to be deleted:
uTriga | event_event_category
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.`

I typed yes and now am getting the error

column app_category.sub_category_ID_id does not exist

column uTriga_category.sub_category_ID_id does not exist

flexxxit
  • 2,440
  • 5
  • 42
  • 69
  • Django syncdb does not modify the db tables for you(https://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb), so probably you should use South migration, or modify the database manually – Xun Yang Aug 21 '12 at 09:45
  • There are [many](http://stackoverflow.com/questions/1605662/django-syncdb-and-an-updated-model), [many](http://stackoverflow.com/questions/11776386/django-get-alter-table-commands-for-a-module), [many](http://stackoverflow.com/questions/11193835/why-is-django-manage-py-syncdb-failing-to-create-new-columns-on-my-development-s) questions that already cover this on SO. syncdb does not alter databases. Use [south](http://south.aeracode.org/). – dgel Aug 21 '12 at 09:48

1 Answers1

0

By default, Django syncdb mechanism doesn't allow model changes. The entire database has to be dropped and recreated. Only new models can be added between two syncb runs.

So you'd probably want to use some 'migration' tool that allows progressive evolutions of the DB schema. This is almost always a very good idea to use one IMHO.

South is the most famous for Django, and so far I've been pretty happy with it.

niconoe
  • 752
  • 5
  • 7
  • I just installed South-0.7.6. Can you please show me how I will use it per the problem in my question ? – flexxxit Aug 21 '12 at 09:52
  • @YehonathanQuartey - You should read the documentation on the south page, then try it yourself. If you have problems while using south, then post a new detailed question. – dgel Aug 21 '12 at 10:09
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – niconoe Aug 21 '12 at 11:43