How to fix database error and ghost migration error in Django?

I get a DatabaseError saying there is no column named playlist and I am trying to figure out how to fix it. I use the south. I deleted the old files in my migration folder and ran:

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

I get this error when I do this:

 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 am not sure how to get rid of this error, since in my migration folder I have only init.py (c) and 0001_initial.py (c); I no longer have the 0002 migration file.

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

 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 tips to fix this?

+4
source share
5 answers

The south stores migration information in a database also in a table called “migration”. [I think this is the name of the table; recording it from memory].

You need to clear this table.

Note

  • Once you clear this table, you need to start the migration again from scratch; straight from the initial migration.
  • It would be nice to make a copy of your database as is before you do this. I assume your code is already versioned.
+1
source

Just run

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

This should remove unnecessary migration from the south_migrationhistory database south_migrationhistory .

+11
source

First, you must decide what happened to disable db and file system synchronization.

Then, if necessary, you can do

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

or

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

as Aydas said, depending on what seems more appropriate. The ignore option is probably less dangerous, although something has already gone astray so you can get to that state.

+3
source

Usually this error occurs because you created the transfer file and performed the migration, this migration file was deleted from your file system (disk)

Thus, you have database changes caused by a migration that no longer exists. Depending on whether you deleted the migration file file of your choice, what you can do; goes further and also removes the changes from the database.

Run the python shell; $ python manage.py shell

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

This will remove the 0002 migration from db. Now you can continue and create / recreate the required migration.

Goodluck, KOMU.

+1
source

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

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

0
source

Source: https://habr.com/ru/post/1442330/


All Articles