【问题标题】:How to fix a Database Error and ghost migration error in Django?如何修复 Django 中的数据库错误和幽灵迁移错误?
【发布时间】:2023-03-27 23:04:01
【问题描述】:

我收到一个 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

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

【问题讨论】:

标签: python django model django-south


【解决方案1】:

直接跑吧

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      您需要清除该表。

      注意

      • 清除该表后,您必须重新从头开始迁移;从最初的迁移开始。
      • 最好在执行此操作之前按原样制作数据库副本。我假设您的代码已经受版本控制。

      【讨论】:

      • 你会推荐 manage.py reset south 还是 manage.py sqlclear app_name?
      【解决方案4】:

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

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

      启动python shell; $python manage.py shell

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

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

      祝你好运, 小木。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2021-09-23
          • 2018-10-21
          • 1970-01-01
          • 2016-04-08
          • 1970-01-01
          • 2015-08-08
          • 2018-08-19
          • 2019-01-09
          • 1970-01-01
          相关资源
          最近更新 更多