Python Django 1.9迁移错误'错误创建新内容类型......'

时间:2016-02-03 14:56:27

标签: python django postgresql model

我正在使用Python 3.4和Django 1.9.1,我正在尝试迁移我的新模型。首先,我在命令提示符下编写:python manage.py makemigrations mission,并且没有问题。但后来我输入了:

python manage.py迁移任务

但是我一直收到关于contenttypes的错误:

'创建新内容类型时出错。在尝试单独迁移应用之前,请确保迁移contenttypes

我已经尝试过查看类似的stackoverflow问题,但没有任何帮助。如何才能停止此错误? statement处的一个解决方案是:

手动从'django_content_type'表中删除列名。例如

'ALTER TABLE django_content_type DROP COLUMN name'

但我该怎么做?到目前为止,我还没有想出如何在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

3 个答案:

答案 0 :(得分:1)

  

'创建新内容类型时出错。在尝试单独迁移应用之前,请确保迁移contenttypes

正如错误所述,您应该在迁移应用之前迁移内容类型应用

python manage.py migrate contenttypes
python manage.py migrate missions

答案 1 :(得分:0)

运行python manage.py makemigrations && python manage.py migrate

如果您提供特定的应用标签(例如“任务”),则运行该应用的迁移。在创建django核心应用程序的表之前,您无法迁移自己的应用程序。

答案 2 :(得分:-1)

我通过删除所有迁移文件并删除数据库来解决我的问题(我目前正在使用sql lite。我只是删除了数据库)。 之后我只运行了makemigration和migrate命令。

我错误地删除了迁移,进行了一些更改并再次迁移。 这会导致问题,因为我的数据库表不再与我的迁移文件同步。