2017-08-08 91 views
0

我的models.py文件,Django的数据库迁移问题 - 缺少 “移民类” 错误

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 

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

+0

@uthay嗨 - 你可以提供更多的代码和信息?如果您使用自动迁移,这通常不会发生 - 您是否创建了自己的迁移?也请发布完整的追溯(不只是最后几行),并且发布模型代码也很有帮助。最后,由于数据库迁移是一个Django问题(DRF刚刚从现有数据库读取),因此rest_framework似乎不会出现问题。 –

+0

Infact堆栈溢出不允许我发布整个回溯。我正在创建自己的迁移,但对于现有的表模型迁移,我也面临同样的问题。 – uthay

+0

自动化迁移是否意味着对传统数据库的“-inspectdb”命令?我对吗 ?无论如何,我不使用自动化。我使用ID MySQL的数据库。 – uthay

回答

0

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

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

从当前的文档(1.11):

Django就在加载时迁移文件(如一个Python模块)对什么是django.db.migrations.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)), 
    ] 
+0

谢谢!问题仅在于不正确的迁移。我删除了迁移文件夹,并尝试过,它的工作。但有一些错误,这不会影响我的执行..(Django_content_type)content_type表中的err.Unknown列'名称'无法删除。这就是错误发生的原因。 – uthay