【问题标题】:django.db.migrations.graph.CircularDependencyError in Django 1.7.1Django 1.7.1 中的 django.db.migrations.graph.CircularDependencyError
【发布时间】:2014-10-29 19:31:04
【问题描述】:

刚刚升级到 Django 1.7.1,正在尝试设置一个全新的开发环境。我运行了 users 迁移 OK,但是当我尝试运行 tweets 迁移时,我得到了

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 106, in handle
    plan = executor.migration_plan(targets)
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/db/migrations/executor.py", line 49, in migration_plan
    for migration in self.loader.graph.forwards_plan(target):
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/db/migrations/graph.py", line 55, in forwards_plan
    return self.dfs(node, lambda x: self.dependencies.get(x, set()))
  File "/Users/libbyh/Documents/virtualenvs/fyl/lib/python2.7/site-packages/django/db/migrations/graph.py", line 105, in dfs
    raise CircularDependencyError()
django.db.migrations.graph.CircularDependencyError

所以我试图追踪循环依赖。以下是可以迁移的模型:

users/models.py

from django.db import models

# Create your models here.
class UsersTweets(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.ForeignKey('users.User')
    tweet = models.ForeignKey('tweets.Tweet')
    source = models.BooleanField() # sent the tweet
    target = models.BooleanField() # received a reply in the tweet

    class Meta:
        managed = True

class User(models.Model):
    id = models.IntegerField(primary_key=True)
    twitter_id = models.CharField(max_length=21, unique=True)
    twitter_name = models.CharField(max_length=55, unique=True)
    fullname = models.CharField(max_length=45)
    followers = models.IntegerField()
    following = models.IntegerField()
    favorites = models.IntegerField()
    tweets = models.IntegerField()
    timezone = models.CharField(max_length=45, blank=True)
    legislator = models.ForeignKey('users.Legislator', blank=True, null=True)

    class Meta:
        managed = True

class Legislator(models.Model):
    id = models.IntegerField(primary_key=True)
    PARTY = (
        ('Democrat','Democrat'),
        ('Republican','Republican'),
        ('Independent','Independent'),
    )
    GENDER = (
        ('M','Man'),
        ('F','Woman'),
    )
    TYPE = (
        ('sen','Senator'),
        ('rep','Representative')
    )
    STATE = (
        ('AK', 'Alaska'),
        ('AL', 'Alabama'),
        ('AR', 'Arkansas'),
        ('AS', 'American Samoa'),
        ('AZ', 'Arizona'),
        ('CA', 'California'),
        ('CO', 'Colorado'),
        ('CT', 'Connecticut'),
        ('DC', 'District of Columbia'),
        ('DE', 'Delaware'),
        ('FL', 'Florida'),
        ('GA', 'Georgia'),
        ('GU', 'Guam'),
        ('HI', 'Hawaii'),
        ('IA', 'Iowa'),
        ('ID', 'Idaho'),
        ('IL', 'Illinois'),
        ('IN', 'Indiana'),
        ('KS', 'Kansas'),
        ('KY', 'Kentucky'),
        ('LA', 'Louisiana'),
        ('MA', 'Massachusetts'),
        ('MD', 'Maryland'),
        ('ME', 'Maine'),
        ('MI', 'Michigan'),
        ('MN', 'Minnesota'),
        ('MO', 'Missouri'),
        ('MP', 'Northern Mariana Islands'),
        ('MS', 'Mississippi'),
        ('MT', 'Montana'),
        ('NA', 'National'),
        ('NC', 'North Carolina'),
        ('ND', 'North Dakota'),
        ('NE', 'Nebraska'),
        ('NH', 'New Hampshire'),
        ('NJ', 'New Jersey'),
        ('NM', 'New Mexico'),
        ('NV', 'Nevada'),
        ('NY', 'New York'),
        ('OH', 'Ohio'),
        ('OK', 'Oklahoma'),
        ('OR', 'Oregon'),
        ('PA', 'Pennsylvania'),
        ('PR', 'Puerto Rico'),
        ('RI', 'Rhode Island'),
        ('SC', 'South Carolina'),
        ('SD', 'South Dakota'),
        ('TN', 'Tennessee'),
        ('TX', 'Texas'),
        ('UT', 'Utah'),
        ('VA', 'Virginia'),
        ('VI', 'Virgin Islands'),
        ('VT', 'Vermont'),
        ('WA', 'Washington'),
        ('WI', 'Wisconsin'),
        ('WV', 'West Virginia'),
        ('WY', 'Wyoming'),
    )
    last_name = models.CharField(max_length=17, blank=True)
    first_name = models.CharField(max_length=11, blank=True)
    gender = models.CharField(max_length=1, blank=True)
    chamber = models.CharField(max_length=3, blank=True)
    state = models.CharField(max_length=2, blank=True)
    party = models.CharField(max_length=11, blank=True)
    url = models.CharField(max_length=36, blank=True)
    address = models.CharField(max_length=55, blank=True)
    phone = models.CharField(max_length=12, blank=True)
    contact_form = models.CharField(max_length=103, blank=True)
    rss_url = models.CharField(max_length=106, blank=True)
    facebook = models.CharField(max_length=27, blank=True)
    facebook_id = models.IntegerField(blank=True, null=True)
    youtube = models.CharField(max_length=20, blank=True)
    youtube_id = models.IntegerField(blank=True, null=True)
    bioguide_id = models.IntegerField(blank=True, null=True)
    thomas_id = models.IntegerField(blank=True, null=True)
    opensecrets_id = models.IntegerField(blank=True, null=True)
    lis_id = models.IntegerField(blank=True, null=True)
    cspan_id = models.IntegerField(blank=True, null=True)
    govtrack_id = models.IntegerField(blank=True, null=True)
    votesmart_id = models.IntegerField(blank=True, null=True)
    ballotpedia_id = models.IntegerField(blank=True, null=True)
    washington_post_id = models.IntegerField(blank=True, null=True)
    icpsr_id = models.IntegerField(blank=True, null=True)
    wikipedia_id = models.CharField(max_length=40, blank=True)

    def _get_name_with_honor(self):
        return '%s. %s %s (%s-%s)' % ((self.chamber).title(), self.first_name, self.last_name, self.party[:1], self.state)
    honor_name = property(_get_name_with_honor) 

    def __unicode__(self):
        return self.last_name

    class Meta:
        managed = True

tweets/models.py

from django.db import models

# Create your models here.
class Tweet(models.Model):
    tweet_id = models.CharField(primary_key=True, max_length=21)
    created_at = models.DateTimeField()
    text = models.CharField(max_length=255)
    source = models.CharField(max_length=255, blank=True)
    location_geo = models.TextField(blank=True) # This field type is a guess.
    location_geo_0 = models.DecimalField(max_digits=14, decimal_places=10, blank=True, null=True)
    location_geo_1 = models.DecimalField(max_digits=14, decimal_places=10, blank=True, null=True)
    iso_language = models.CharField(max_length=3)
    user = models.ManyToManyField('users.User', through = "users.UsersTweets")
    class Meta:
        managed = True

我检查了another StackOverflow question about this error,但我没有从我的错误中得到任何关于依赖关系可能在哪里的信息。尝试关注this advice,但不确定如何编辑swappable_dependency。有任何想法吗?谢谢!

【问题讨论】:

  • 仍然没有,很遗憾。不过好久没看了。

标签: mysql django


【解决方案1】:
1. In tweets/models.py comment #user = models.ManyToManyField('users.User', through = "users.UsersTweets")
2. makemigrations tweets
3. makemigrations users
4. UNcomment in tweets/models.py user = models.ManyToManyField('users.User', through = "users.UsersTweets")
5. makemigrations tweets
6. migrate

【讨论】:

  • 谢谢@TitanFighter。我试试看。
猜你喜欢
  • 2015-05-14
  • 2015-01-31
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
相关资源
最近更新 更多