Forums

Django OperationalError after adding a new attribute to an existing model

Hello, I wanted to add a new attribute to my existing Django model, but I get:

Exception Type: OperationalError
Exception Value: table myapp_screen has no column named caption

I know I can't just add new values to classes when there are existing objects, but I've flushed my database before that and still I am getting this exception. Here's how my models file look like:

from django.db import models
import os

def get_screens_upload_path(instance, filename):
    return os.path.join(
      "screens", "%s" % instance.project.tag, filename)

class Project(models.Model):
    name = models.CharField(max_length=50)
    tag = models.CharField(max_length=20)
    date_started = models.DateField()
    date_finished = models.DateField()
    description = models.TextField()
    link = models.URLField(blank=True)

    def __str__(self):
        return self.name

class Screen(models.Model):
    project = models.ForeignKey(Project)
    caption = models.CharField(max_length=50)
    image = models.ImageField(upload_to=get_screens_upload_path)

Everything was fine before I added 'caption' to my Screen model, then it all collapsed. Moreover, when i run

python manage.py inspectdb

it returns me my model without new 'caption' attribute:

class MyappScreen(models.Model):
    id = models.IntegerField(primary_key=True)
    project = models.ForeignKey(MyappProject)
    image = models.CharField(max_length=100)
    class Meta:
        db_table = u'myapp_screen'

I am guessing it has something to do with my migrations, but what I thought was that pythonanywhere is migrating every changes each time I reload my web. Isn't that so?

@EDIT: I forgot to say, that

python manage.py syncdb

says there is no problem :(

When you say you flushed your db, did you delete your whole db and then run syncdb again?

PythonAnywhere does not migrate the changes for you- it just restarts the webapp process.

You will have to use south etc to do the migration (unless you delete the whole db and run syncdb).

What I did in few steps was:

1) Save changes to models.py with the new caption attribute in Screen model.

2) Reload the web (just in case).

3) Flush my database and then sync it.

4) Reload the web once more.

But that didn't fix anything. I am still getting the same OperationalError.

@EDIT: I just tried to do some migrations with south, firstly I included 'south' in INSTALLED_APPS in settings.py, then I synced my database and then ran:

python manage.py schemamigration myapp --initial

this went fine but then came

python manage.py migrate myapp

which raised an exception like this

Error in migration: myapp:0001_initial
django.db.utils.DatabaseError: table "myapp_project" already exists

so my problem still isn't solved. Please help!

what do you mean by flush your db? Do you mean you deleted your whole db and then ran syncdb again?

By flushing I mean

python manage.py flush

I think that's what deletes old and creates new db, right?

See this. I would just delete the whole db manually.

You were right, deleting the database manually and then syncing it was the solution! Thank you very much!