Django迁移抱怨循环依赖

时间:2016-02-04 15:11:54

标签: python django django-models migration

我有以下型号:

from __future__ import unicode_literals

from django.db import models


class Category(models.Model):
    """Podcast categories"""
    name = models.CharField(max_length=20, primary_key=True)
    visible = models.BooleanField(default=False)
    parent = models.OnToOneField("self", primary_key=True)

    def __str__(self):
        return self.name


class Podcast(models.Model):
    url = models.CharField(max_length=255, primary_key=True)
    link = models.CharField(max_length=255)
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=200, null=True)
    description = models.TextField()
    image = models.CharField(max_length=255, null=True)
    explicit = models.SmallIntegerField(default=-1, choices=[(-1, "not set",),(0, "clean"), (1, "explicit")])
    owner_email = models.CharField(max_length=200)
    owner_name = models.CharField(max_length=200)
    tags = models.CharField(max_length=255)
    categories = models.ManyToManyField(Category)
    last_fetched = models.DateTimeField()

    def __str__(self):
        return self.title


class Episode(models.Model):
    podcast = models.ForeignKey(Podcast, null=False)
    title = models.CharField(max_length=255)
    link = models.CharField(max_length=255)
    subtitle = models.CharField(max_length=255)
    summary = models.CharField(max_length=500)
    description = models.TextField()
    content = models.TextField()
    enclosure_type = models.CharField(max_length=20)
    enclosure_url = models.CharField(max_length=255)
    duration = models.PositiveIntegerField(default=0)
    explicit = models.SmallIntegerField(choices=[(-1, "not set",),(0, "clean"), (1, "explicit")])
    author = models.CharField(max_length=255)
    image = models.CharField(max_length=255)
    published = models.DateTimeField()

    def __str__(self):
        return self.title

当我运行python manage.py makemigrations时,我收到此错误:

Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 186, in handle
    changes = autodetector.changes(graph=executor.loader.graph)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 45, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 184, in _detect_changes
    self._sort_migrations()
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 334, in _sort_migrations
    self.generated_operations[app_label] = stable_topological_sort(ops, dependency_graph)
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/topological_sort.py", line 27, in stable_topological_sort
    for layer in topological_sort_as_sets(dependency_graph):
  File "/usr/local/lib/python2.7/site-packages/django/db/migrations/topological_sort.py", line 16, in topological_sort_as_sets
    ', '.join(repr(x) for x in todo.items())))
ValueError: Cyclic dependency in graph: (<AddField  field=<django.db.models.fields.related.ManyToManyField: categories>, name=u'categories', model_name=u'podcast'>, set([<CreateModel  fields=[(u'name', <django.db.models.fields.CharField>), (u'visible', <django.db.models.fields.BooleanField>), (u'parent', <django.db.models.fields.related.OneToOneField>)], bases=(<class 'django.db.models.base.Model'>,), managers=[], options={}, name=u'Category'>])), (<CreateModel  fields=[(u'name', <django.db.models.fields.CharField>), (u'visible', <django.db.models.fields.BooleanField>), (u'parent', <django.db.models.fields.related.OneToOneField>)], bases=(<class 'django.db.models.base.Model'>,), managers=[], options={}, name=u'Category'>, set([<CreateModel  fields=[(u'name', <django.db.models.fields.CharField>), (u'visible', <django.db.models.fields.BooleanField>), (u'parent', <django.db.models.fields.related.OneToOneField>)], bases=(<class 'django.db.models.base.Model'>,), managers=[], options={}, name=u'Category'>]))

看起来这种情况发生是因为父类指的是相同的模型,但我不知道如何修复它。

0 个答案:

没有答案