John Eipe
John Eipe

Reputation: 11228

error after adding a new field in Django Models

My UserProfile was working fine until I changed the model and form. I added a DateField in model and updated my Forms.py and template. Also did a syncdb.

profile/models.py

class UserProfiles(models.Model):
    user = models.OneToOneField(User)
    #other fields here
    birthday = models.DateField()

profile/forms.py

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfiles
        fields = ('some_field', 'birthday', 'otherfields')

profile/views.py

def editprofile(request):
    return render_to_response('profile_edit.html', {'form':UserProfileForm()}, context_instance=RequestContext(request))

This is the error it is throwing.

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "E:\django-sample\proschools\..\proschools\profile\views.py" in generateprofile
  12.             userprofile = UserProfiles.objects.get(user=request.user)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
  344.         num = len(clone)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in __len__
  82.                 self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py" in iterator
  273.         for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  680.         for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  735.         cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  34.             return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py" in execute
  86.             return self.cursor.execute(query, args)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py" in execute
  174.             self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: OperationalError at /profile/
Exception Value: (1054, "Unknown column 'profile_userprofiles.birthday' in 'field list'")

Upvotes: 2

Views: 2813

Answers (3)

Priyank Patel
Priyank Patel

Reputation: 3535

http://code.google.com/p/django-evolution/

When you run ./manage.py syncdb, Django will look for any new models that have been defined, and add a database table to represent those new models. However, if you make a change to an existing model, ./manage.py syncdb will not make any changes to the database.

This is where Django Evolution fits in. Django Evolution is an extension to Django that allows you to track changes in your models over time, and to update the database to reflect those changes.

Upvotes: 2

dgel
dgel

Reputation: 16796

Syncdb won't automatically create new fields for you. You would have to entirely delete the table and run syncdb to apply the schema changes to the DB.

Most django developers use a third party app called south to handle these types of migrations. South will let you add fields and migrate the database without having to recreate it or manually alter the database.

Upvotes: 1

Joseph Victor Zammit
Joseph Victor Zammit

Reputation: 15310

If the table existed before you added the new column syncdb will not add new columns to it! syncdb does not alter existing tables. Consider using Django south or else add the column manually.

Upvotes: 2

Related Questions