pythondjangodjango-modelsdjango-syncdbsyncdb

table UserAndPlace_userprofile has no column named gender


While I save UserProfile into a database,

there's an error "UserProfile has no column named gender error".

Here's my models.py

 """ UserProfile : user information """
 class UserProfile(models.Model):
     # User basic inherits : username, password, email
     user = models.OneToOneField(User)
     # Profile information
     nickname = models.CharField(max_length=63, unique=True, null=False)
     url = models.URLField(blank=True, null=True)
     birth = models.DateField(null=True) # TODO change null = false
     gender = models.CharField(max_length=15, null=False)
     nation = models.CharField(max_length=63, null=True)
     region = models.CharField(max_length=63, null=True)
     description = models.TextField(blank=True, null=True)
     # ImageField: http://goo.gl/ZQEG4e
     avatar = models.ImageField(upload_to='/avatar/')
     # Foreign Keys
     tag = models.ManyToManyField(TagCloud)

and while I tested it from ./python models.py shell I typed

 > from UserAndPlace.models import *
 > u1 = User.objects.get(username="user1")
 > u1.username
   u'user1'
 > p1 = UserProfile(user=u1, gender="female")
 > p1.user.username
   u'user1'
 > p1.gender
   u'female'
 > p1.save()


 OperationalError                          Traceback (most recent call last)
 <ipython-input-9-e08e160cd285> in <module>()
 ----> 1 p1.save()
 ....
 OperationalError: table UserAndPlace_userprofile has no column named gender

I did python manage.py syncdb and also check errors with python manage.py sql UserAndPlace

How can I fix this errors?

Thanks for your help in advance.


Solution

  • syncdb will not create or modify columns if they already exist in the database. If you already had the model and then you added the gender column, running syncdb will not do anything.

    You need to drop the table and re-create it again. If you are using sqlite, simply delete the database file and run syncdb.

    If you are using another database, run manage.py sqlclear yourapp and this will give you the SQL you need to execute to reset the tables for the app yourapp (make sure you replace yourapp with the name of the application that has the profile model).

    Then, run syncdb again to recreate your models.