sharataka
sharataka

Reputation: 5132

How to fix a Database Error and ghost migration error in Django?

I am getting an DatabaseError saying no column named playlist exists and I'm trying to figure out how to fix it. I'm using South. I deleted the old files in the my migrations folder and ran:

python manage.py schemamigration app_name --initial
python manage.py migrate reserve

I get this error when I do that:

south.exceptions.GhostMigrations: 

 ! These migrations are in the database but not on disk:
    <reserve: 0002_initial>
 ! I'm not trusting myself; either fix this yourself by fiddling
 ! with the south_migrationhistory table, or pass --delete-ghost-migrations
 ! to South to have it delete ALL of these records (this may not be good). 

I'm not sure how to get rid of this error, since in my migrations folder I only have init.py(c) and 0001_initial.py(c); I don't have 0002 migration file anymore.

When I try runserver and click "add playlist" in the admin, this is when I get the DatabaseError. If it helps, my models.py is:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    def __unicode__(self):
        return self.user

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)         

class Playlist(models.Model):
    playlist = models.CharField('Playlist', max_length = 2000, null=True, blank=True)
    def __unicode__(self):
        return self.playlist

class Video(models.Model):
    video_url = models.URLField('Link to video', max_length = 200, null=True, blank=True)
    def __unicode__(self):
        return self.video_url

class UserPlaylist(models.Model):
    profile = models.ForeignKey(User)
    playlist = models.ForeignKey(Playlist)
    def __unicode__(self):
        return self.playlist

class Videoplaylist(models.Model):
    video = models.ForeignKey(Video)
    playlist = models.ForeignKey(UserPlaylist)
    def __unicode__(self):
        return self.playlist

Any advice on how to fix this?

Upvotes: 4

Views: 15213

Answers (5)

samresh kr Jha
samresh kr Jha

Reputation: 33

Just run the command where manage.py file is present in your directory

./manage.py migrate appname --delete-ghost-migrations

Upvotes: 0

Komu
Komu

Reputation: 15018

Usually this error happens because, you created a migration file and did the migration then, that migration file was deleted from your file system(disk)

So you have changes in your database caused by a migration that no longer exists. Depending on whether you deleted the migration file file by choice, what you can do; is go ahead and also delete the changes from the database.

Start the python shell; $ python manage.py shell

>>from south.models import MigrationHistory
>>MigrationHistory.objects.filter(migration='0002_initial').delete()

That will have deleted the 0002 migration from the db. You can now go ahead and create/recreate the migration you want.

Goodluck, Komu.

Upvotes: 1

Terry Brown
Terry Brown

Reputation: 1330

First, you should work out what happened to get the db and filesystem out of sync.

Then, if appropriate, you can do

python manage.py migrate reserve --ignore-ghost-migrations

or

python manage.py migrate reserve --delete-ghost-migrations

as Aidas said, whichever seems more appropriate. The ignore option is probably less risky, although something has already gone astray for you to get to this state.

Upvotes: 3

Aidas Bendoraitis
Aidas Bendoraitis

Reputation: 4003

Just run

python manage.py migrate reserve --delete-ghost-migrations

This should remove non existing migration from the database table south_migrationhistory.

Upvotes: 12

Ngure Nyaga
Ngure Nyaga

Reputation: 3027

South stores migration information in the database too, in a table called "migrations". [ I think thats the table name; writing this from memory ].

You need to clear that table out.

Note

  • Once you clear that table out, you have to start the migrations again from scratch; right from the initial migration.
  • It would be a good idea to make a copy of your database as-is before you do this. I assume that your code is already version controlled.

Upvotes: 1

Related Questions