0

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.

sogo
  • 351
  • 5
  • 20

2 Answers2

1

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I already drop my table using 'sqlclear' but occurred same error. Anything else I do, sir? – sogo Jul 19 '14 at 13:42
  • 1
    Just typing it will not clear it, you need to execute it. Try `python manage.py dbshell < python manage.py sqlclear yourapp`, then run `python manage.py syncdb` and if you are using south, `python manage.py migrate`. – Burhan Khalid Jul 19 '14 at 13:43
0

Have you done a python manage.py syncdb after you made any changes to your model? This error usually happens when the model and the database are out of sync.