2017-08-08 94 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