4

我收到一个 DatabaseError,说不存在名为 playlist 的列,我正在尝试找出解决方法。我用的是南方。我删除了迁移文件夹中的旧文件并运行:

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

执行此操作时出现此错误:

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). 

我不知道如何摆脱这个错误,因为在我的迁移文件夹中我只有 init.py(c) 和 0001_initial.py(c); 我没有 0002 迁移文件了。

当我尝试运行服务器并在管理员中单击“添加播放列表”时,这是我收到 DatabaseError 的时候。如果有帮助,我的 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

关于如何解决这个问题的任何建议?

4

5 回答 5

12

赶紧跑

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

这应该从数据库表中删除不存在的迁移south_migrationhistory

于 2012-10-26T15:34:48.840 回答
3

首先,您应该找出导致数据库和文件系统不同步的原因。

然后,如果合适的话,你可以做

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

或者

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

正如艾达斯所说,以更合适的为准。该ignore选项的风险可能较小,尽管您已经误入歧途以达到这种状态。

于 2013-11-19T22:58:28.487 回答
1

South 也在数据库中将迁移信息存储在名为“迁移”的表中。[我认为那是表名;从记忆中写下来]。

您需要清除该表。

笔记

  • 清除该表后,您必须从头开始重新开始迁移;从最初的迁移开始。
  • 在执行此操作之前按原样制作数据库副本是个好主意。我假设您的代码已经受版本控制。
于 2012-10-26T15:15:46.073 回答
1

通常会发生此错误,因为您创建了一个迁移文件并随后进行了迁移,该迁移文件已从您的文件系统(磁盘)中删除

因此,您的数据库发生了由不再存在的迁移引起的更改。取决于您是否选择删除了迁移文件文件,您可以做什么;继续并从数据库中删除更改。

启动python外壳;$python manage.py shell

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

这将从数据库中删除 0002 迁移。您现在可以继续创建/重新创建所需的迁移。

祝你好运,科穆。

于 2014-02-21T10:30:55.103 回答
0

只需运行目录中存在 manage.py 文件的命令

./manage.py 迁移应用程序名 --delete-ghost-migrations

于 2017-06-07T07:27:18.400 回答