2016-02-03 71 views
1

我使用的是Python 3.4和Django 1.9.1,我試圖遷移我的新模型。首先我寫下:在命令提示符下運行python manage.py makemigrations任務,並且沒有問題。但後來我輸入:Python的Django 1.9遷移錯誤'錯誤創建新的內容類型...'

蟒蛇manage.py遷移任務

但我不斷收到有關CONTENTTYPES此錯誤:

「錯誤創建新的內容類型。請確保在嘗試單獨遷移應用程序之前遷移contenttypes'

我試着在類似的stackoverflow問題中四處尋找,但沒有任何幫助。我如何得到這個錯誤停止? Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually的一個這樣的解決方案聲明:

通過手動刪除'django_content_type'表中的列名稱。例如。

'ALTER TABLE django_content_type DROP列名'

但我怎麼做呢?到目前爲止,我還沒有想出如何在Django中使用像這樣的純SQL語句。我正在使用PostgreSQL,並且不斷遇到各種各樣的問題。請幫忙。以下是我的任務型號:

from django_pg import models 
from django.contrib.auth.models import User 


# Create your models here. 
OUTCOME_CHOICES = (
    ('U', 'Unsuccessful'), 
    ('S', 'Successful'), 
) 

STATS_CHOICES = (
    ('1', 'Extremely Low'), 
    ('2', 'Very Low'), 
    ('3', 'Low'), 
    ('4', 'Average'), 
    ('5', 'Good'), 
    ('6', 'Above Average'), 
    ('7', 'High'), 
    ('8', 'Very High'), 
    ('9', 'Super Human'), 
    ('10', 'Above and Beyond'), 

) 
class Hero(models.Model): 
    codename = models.CharField(max_length = 20) 

    def __str__(self): 
     return self.codename 

class Team (models.Model): 
    name = models.CharField(max_length = 20) 
    address = models.CharField(max_length = 100) 
    description = models.TextField 
    leader = models.CharField(max_length = 20) 
    members = models.TextField 

class Customer(models.Model): 
    first_name = models.CharField(max_length = 25) 
    surname = models.CharField(max_length = 30) 
    address = models.CharField(max_length = 100) 
    citizenship = models.CharField(max_length = 40) 

class Mission(models.Model): 
    customer = models.ForeignKey('Customer') 
    description = models.TextField 
    location = models.CharField(max_length = 50) 
    difficulty = models.CharField(max_length = 20) 

    def __str__(self): 
     return self.description, self.location, self.difficulty 


class Alias(models.Model): 
    hero = models.ForeignKey('Hero') 
    first_name = models.CharField(max_length = 25) 
    surname = models.CharField(max_length = 30) 
    former_codenames = models.TextField 
    occupation = models.CharField(max_length = 30) 
    address = models.CharField(max_length = 100) 
    citizenship = models.CharField(max_length = 30) 

class HeroStats(models.Model): 
    hero = models.ForeignKey('Hero') 
    height = models.CharField(max_length = 10) 
    weight = models.CharField(max_length = 10) 
    powers = models.TextField 
    intelligence = models.CharField(max_length = 5, choices = STATS_CHOICES) 
    stamina = models.CharField(max_length = 5, choices = STATS_CHOICES) 
    strength = models.CharField(max_length = 5, choices = STATS_CHOICES) 

class HeroStatus(models.Model): 
    hero = models.ForeignKey('Hero') 
    hero = models.IntegerField 
    mission = models.IntegerField 
    team = models.IntegerField 

    def __str__(self): 
     return "{0} is registered in team {1}, and is currenly on mission {3}".format(self.hero, self.team, self.mission) 

class Report(models.Model): 
    mission = models.ForeignKey('Mission') 
    outcome = models.CharField(max_length = 15, choices = OUTCOME_CHOICES) 
    comments = models.TextField 

    def __str__(self): 
     return self.outcome 

回答

0

'創建新內容類型時出錯。請確保CONTENTTYPES試圖單獨遷移應用程序」

由於錯誤說之前遷移,您應該遷移CONTENTTYPES應用之前您遷移應用程序。

python manage.py migrate contenttypes 
python manage.py migrate missions 
+0

嘗試過,但仍然存在錯誤。我在問題中解釋了同樣的情況。只是我正在使用django 1.8。 –

+0

@SahanaPrabhakar這個問題已經過了一年多了,如果它不適合你,我建議你問一個新問題,並且包含足夠的細節來重現問題。 – Alasdair

0

run python manage.py makemigrations && python manage.py migrate

如果您提供特定的應用程序標籤(例如「任務」),您將只爲運行爲該應用程序運行遷移。在創建django核心應用程序的表之前,您無法遷移自己的應用程序。

-1

我刪除我的所有遷移文件和刪除我的數據庫解決我的問題(我目前使用SQL精簡版,我只是刪除DB)。 之後,我只需運行makemigration和migrate命令。

我犯了刪除遷移的錯誤,做了一些更改並重新遷移。 這會導致問題,因爲我的數據庫表不再與我的遷移文件同步。