Django数据库迁移问题 - 缺少“迁移类”错误

时间:2017-08-08 09:53:37

标签: django django-models

我的models.py文件,

class Dag(models.Model):
    dag_id = models.CharField(primary_key=True, max_length=250)
    is_paused = models.IntegerField(blank=True, null=True)
    is_subdag = models.IntegerField(blank=True, null=True)
    is_active = models.IntegerField(blank=True, null=True)
    last_scheduler_run = models.DateTimeField(blank=True, null=True)
    last_pickled = models.DateTimeField(blank=True, null=True)
    last_expired = models.DateTimeField(blank=True, null=True)
    scheduler_lock = models.IntegerField(blank=True, null=True)
    pickle_id = models.IntegerField(blank=True, null=True)
    fileloc = models.CharField(blank=True, max_length=250)
    owners = models.CharField(blank=True, max_length=250)

    def _str_(self):
        return self.dag_id 

    class Meta:
        managed = True
        db_table = 'dag'

我的serializers.py文件,

class Dag_api_serializers(ModelSerializer):
class Meta:
    model = Dag
    fields = ('dag_id','is_paused','is_active','is_subdag','last_scheduler_run','last_pickled','last_expired','scheduler_lock','pickle_id','fileloc','owners')

在给出命令python manage.py migratemakemigrations时发生以下错误。我的模型中有一个db表,我希望它们通过此命令进行迁移...这里我提到了最后几行错误消息

    Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\env\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\env\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\env\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\env\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\env\lib\site-packages\django\core\management\commands\makemigrations.py", line 96, in handle
    loader = MigrationLoader(None, ignore_no_migrations=True)
  File "C:\env\lib\site-packages\django\db\migrations\loader.py", line 52, in __init__
    self.build_graph()
  File "C:\env\lib\site-packages\django\db\migrations\loader.py", line 203, in build_graph
    self.load_disk()
  File "C:\env\lib\site-packages\django\db\migrations\loader.py", line 117, in load_disk
    "Migration %s in app %s has no Migration class" % (migration_name, app_config.label)
django.db.migrations.exceptions.BadMigrationError: Migration serializers in app snippets has no Migration class

在模型中,我指的是已经存在的表格。

2 个答案:

答案 0 :(得分:0)

查看Django docs for Migrations,特别是迁移文件的格式。如果没有看到您的迁移代码(您在手动编写的评论中说过),我猜测迁移尚未正确声明。

错误告诉您应用程序中的迁移(“序列化程序”)(称为“代码段”)没有迁移类,因此无法处理迁移。如果您按照文档并正确声明迁移,那么它可能会解决您的问题。

从当前的文档(1.11):

  

Django在加载迁移文件(作为Python模块)时所寻找的是django.db.migrations.Migration的子类,称为Migration。然后它检查这个对象有四个属性,大部分时间只使用其中两个......

EG:

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [('migrations', '0001_initial')]

    operations = [
        migrations.DeleteModel('Tribble'),
        migrations.AddField('Author', 'rating', models.IntegerField(default=0)),
    ]

答案 1 :(得分:0)

嗯,这个错误只发生在你已经有了 makemigrations 但还没有迁移文件并且后来删除了那个模型类时,所以为了解决这个问题,你需要在那个应用程序文件夹中找到迁移文件夹,然后选择您尚未迁移或最近解雇的迁移文件!只需删除该文件并再次重新启动服务器:),我用这种方式自己解决了这个问题,所以这也可能对您有所帮助,最好是创建一个新应用程序并进行迁移。