我在应用迁移时收到错误django.db.migrations.graph.CircularDependencyError
。值得注意的是,我从一个空数据库开始。
我还设法归结为导致错误的类,这似乎是OAuthAccessToken
。
class OAuthAccessToken(Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='oauth_access_tokens',
help_text=_('User'))
provider = models.ForeignKey(Provider, related_name='oauth_access_tokens',
help_text=_('OAuth provider'))
token = fields.SafeRandomField(max_length=32,
help_text=_('Access token'))
上述代码仅在我评论provider
声明时才有效。 Provider
类来自allaccess框架。这是它的声明:
@python_2_unicode_compatible
class Provider(models.Model):
"Configuration for OAuth provider."
name = models.CharField(max_length=50, unique=True)
request_token_url = models.CharField(blank=True, max_length=255)
authorization_url = models.CharField(max_length=255)
access_token_url = models.CharField(max_length=255)
profile_url = models.CharField(max_length=255)
consumer_key = EncryptedField(blank=True, null=True, default=None)
consumer_secret = EncryptedField(blank=True, null=True, default=None)
让代码工作的另一种方法是从我的设置文件中删除AUTH_USER_MODEL = 'api.User'
,在这种情况下第一个代码示例可以正常工作。
问题是,我检查了迁移并找到了这个:
migrations.CreateModel(
name='OAuthAccessToken',
fields=[
('uid', rest.fields.IDField(default=rest.fields.GenerateID(15, b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), max_length=15, serialize=False, primary_key=True, help_text='Server generated public unique ID')),
('creation_date', models.DateTimeField(help_text='Creation date', auto_now_add=True)),
('last_update', models.DateTimeField(help_text='Last update', auto_now=True)),
('token', rest.fields.SafeRandomField(default=rest.fields.GenerateID(32, b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), help_text='Access token', max_length=32)),
('provider', models.ForeignKey(related_name='oauth_access_tokens', to='allaccess.Provider', help_text='OAuth provider')),
('user', models.OneToOneField(related_name='oauth_access_tokens', to=settings.AUTH_USER_MODEL, help_text='User')),
],
options={
'abstract': False,
},
bases=(models.Model,),
),
但我没有看到循环依赖的位置。
编辑:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 106, in handle
plan = executor.migration_plan(targets)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 49, in migration_plan
for migration in self.loader.graph.forwards_plan(target):
File "/Library/Python/2.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 "/Library/Python/2.7/site-packages/django/db/migrations/graph.py", line 105, in dfs
raise CircularDependencyError()
django.db.migrations.graph.CircularDependencyError
编辑:使用django 1.7.7更新
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 106, in handle
plan = executor.migration_plan(targets)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 54, in migration_plan
for migration in self.loader.graph.forwards_plan(target):
File "/Library/Python/2.7/site-packages/django/db/migrations/graph.py", line 60, in forwards_plan
return self.dfs(node, lambda x: self.dependencies.get(x, set()))
File "/Library/Python/2.7/site-packages/django/db/migrations/graph.py", line 124, in dfs
self.ensure_not_cyclic(start, get_children)
File "/Library/Python/2.7/site-packages/django/db/migrations/graph.py", line 112, in ensure_not_cyclic
raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.graph.CircularDependencyError: api.0001_initial, allaccess.0001_initial
答案 0 :(得分:0)
碰巧我需要重构代码并创建一个新的应用程序来包含一些模型来解决这个问题。 Django然后创建了几个迁移(如0002,0003),因此能够解决循环依赖。