Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

South database error: relation already exists

I recently added South to an existing Django project. I went through the whole

python manage.py syncdb
python manage.py convert_to_south myapp
python manage.py migrate myapp 0001 --fake

process according to the last comment on this ticket (because I have a custom user model).

Then I think I did a schemamigration and migrate? I don't totally remember, but what I ended up with were two migration files called 0001_initial.py and 0002_initial.py, which didn't seem exactly right.

Today I tried adding a field to one of my models and migrating like so:

 $ python manage.py schemamigration myapp --auto
 ? The field 'Photo.main_person' does not have a default specified, yet is NOT NULL.
 ? Since you are adding this field, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now, and add a default to the field in models.py
 ?  2. Specify a one-off value to use for existing columns now
 ? Please select a choice: 2
 ? Please enter Python code for your one-off default value.
 ? The datetime module is available, so you can do e.g. datetime.date.today()

 >>> 1
 + Added field main_person on myapp.Photo
Created 0003_auto__add_field_photo_main_person.py. You can now apply this migration with: ./manage.py migrate myapp

$ python manage.py migrate myapp
Running migrations for myapp:
 - Migrating forwards to 0003_auto__add_field_photo_main_person.
 > myapp:0002_initial
FATAL ERROR - The following SQL query failed: CREATE TABLE "myapp_patient" ("id" serial NOT NULL PRIMARY KEY, "password" varchar(128) NOT NULL, "last_login" timestamp with time zone NOT NULL, "email" varchar(255) NOT NULL UNIQUE, "first_name" varchar(100) NOT NULL, "last_name" varchar(100) NOT NULL, "is_active" boolean NOT NULL, "is_admin" boolean NOT NULL, "is_staff" boolean NOT NULL)
The error was: relation "myapp_patient" already exists

Error in migration: myapp:0002_initial
DatabaseError: relation "myapp_patient" already exists

So it created the migration 0003_auto__add_field_photo_main_person but it seems it wasn't able to get past the second migration. Should/can I just delete the second migration file? It's exactly the same as the first one, which appears to be what's causing the problem, but I'm not well-versed enough in South to know if that's a good idea.

like image 915
snorthway Avatar asked Jan 12 '23 22:01

snorthway


1 Answers

You must have created the second migration with --initial, so the name is 002_initial.py. You should have created it with --auto. What happenned in the process is, south thought the second migration as the initial migration, because you passed --initial. So, it wrote command in the migration file expecting that the table myapp_patient is not created yet.

Also, you must not have run the second migration till now otherwise you would have got this error the first time you tried to run the second migration.

If your second migration is exactly the same as first migration, you can just delete it.

Other option you have is to --fake the second migration.

After faking second migration, you can just do usual migration, and it will work

http://agiliq.com/blog/2012/01/south/ might help clear some concepts of south :)

like image 183
Akshar Raaj Avatar answered Jan 19 '23 13:01

Akshar Raaj