H H H
H H H

Reputation: 529

How to properly add fields to models with South?

I have a django app called Locations and in its models.py there are 2 models:

class City(models.Model):
 ...

class Country(models.Model):
 ...

I did python manage.py schemamigration Locations --initial and then python manage.py migrate Locations. Everything worked fine.

Then I added 2 fields to City and did python manage.py schemamigration Locations --auto and it said:

Then when I did python manage.py migrate Locations, I got:

Running migrations for Locations:
 - Migrating forwards to 0003_auto__del_field_country_cover_image__add_field_city_lng__add_field_cit.
 > Locations:0001_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "Locations_country" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(100) NOT NULL UNIQUE, "slug" varchar(50) NOT NULL, "image" varchar(100) NOT NULL, "flag" varchar(100) NOT NULL)
The error was: relation "Locations_country" already exists

Error in migration: Locations:0001_initial
DatabaseError: relation "Locations_country" already exists

I always keep getting this error. Am I doing something wrong?

Then I did python manage.py migrate Locations 0003 --fake and this was the output:

- Soft matched migration 0003 to 0003_auto__del_field_country_cover_image__add_field_city_lng__add_field_cit.
Running migrations for Locations:
 - Migrating forwards to 0003_auto__del_field_country_cover_image__add_field_city_lng__add_field_cit.
 > Locations:0001_initial
   (faked)
 > Locations:0002_auto__add_field_city_lng__add_field_city_ltd
   (faked)
 > Locations:0002_auto__add_location__add_field_country_cover_image
   (faked)
 > Locations:0003_auto__del_field_country_cover_image__add_field_city_lng__add_field_cit
   (faked)

Now when I do python manage.py migrate Locations it says:

Running migrations for Locations:
- Nothing to migrate.
 - Loading initial data for Locations.
Installed 0 object(s) from 0 fixture(s)

And those 2 fields have not been added. Whats going on? Whats the correct way to add/delete fields?

I have read the basic South documentation, please point to me if I have missed out something.

Thanks.

Upvotes: 0

Views: 149

Answers (1)

Aamir Rind
Aamir Rind

Reputation: 39659

Delete 0002 and 0003 migrations files. And then roll back to 0001 by doing:

python manage.py migrate Locations 0001 --fake --delete-ghost-migrations

After that run schemamigration and migrate normally.

(During discussion with OP it was first cleared that 0002 and 0003 was never reflected to database, so it is no harm to delete those migration files from disk)

Upvotes: 2

Related Questions