18

I've run into the same issue presented by the commenter here: Django South - table already exists

There was no follow-up, so I thought I'd post a new question. I have a Django app whose migrations I manage with South. I added a field to my model then ran

./manage schemamigration my_app --auto

which ran as expected. Running

./manage migrate my_app

however, resulted in an error indicating that the table associated with the model I changed already exists. This led me to the above linked question, so running

./manage migrate my_app --fake

resolved the table error, but now I'm getting a Django error that the column associated with the new field does not exist.

./manage sqlall my_app

shows the schema to be as expected.

Any thoughts on how to remedy this are appreciated!

Community
  • 1
  • 1
Drew Cummins
  • 355
  • 2
  • 10

3 Answers3

34

Probably easiest way for you will be to start migrations from scratch.

Delete all migrations/* files for the app which you try to fix. Restore your models.py to the state which is at the moment on the database (by the help of version control tools, or just comment out the new fields). Then initialize migrations:

manage.py migrate my_app --delete-ghost-migrations
manage.py schemamigration my_app --init
manage.py migrate my_app --fake

This will create a record in migrations of what current database structure looks like.

Now add your changes to models.py and south will now what has changed:

manage.py schemamigration my_app --auto
manage.py migrate my_app
Ski
  • 14,197
  • 3
  • 54
  • 64
  • Make also sure to delete all database entries about schema migrations south has made before as well (if you screwed up). @drew: your main mistake was that you should have made the initali migration and run `./manage migrate my_app --fake` BEFORE adding the new field to the model! – Bernhard Vallant Nov 07 '10 at 18:59
  • 1
    @lazerscience `manage.py migrate my_app --delete-ghost-migrations` does the thing. – Ski Nov 07 '10 at 19:01
  • Thank you so much. South can get complicated at times. – Robeezy Jan 04 '14 at 02:20
6

Something else to keep an eye out for: you will often get this error (DatabaseError: no such column: appname_model.fieldname) if you are using a default value in a ForeignKey relation, and you add a field to that FK model.

Something like (in your models.py):

class MyAppModel(models.Model):
    my_foreign_key = models.ForeignKey(FkModel,
                                       default=lambda: FkModel.objects.get(id=1),
                                       null=True)

Then in a new migration, you add a new field to your FkModel:

class FkModel(models.Model):
    new_field = models.IntegerField('New Field Name', blank=True, null=True)

You will get an error when running a South schemamigration:

DatabaseError: no such column: myappmodel_fkmodel.new_field

You can solve this by making sure your initial South migration includes this default value lambda function, but then remove the default value in the next migration.

This has bitten me in the past. Hopefully it will help someone in the future.

tatlar
  • 3,080
  • 2
  • 29
  • 40
0

for beginners or learners if ur stucked here and dont mind DATA LOSE(all) then just delete db.sqlit3 file and re run server it should reset the sqlite