Django 迁移失败并显示“__fake__.DoesNotExist:权限匹配查询不存在”。
Posted
技术标签:
【中文标题】Django 迁移失败并显示“__fake__.DoesNotExist:权限匹配查询不存在”。【英文标题】:Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist." 【发布时间】:2015-10-10 22:40:27 【问题描述】:在一个 Django 1.8 项目中,我有一个运行良好的迁移,when it had the following code:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.conf import settings
def update_site_forward(apps, schema_editor):
"""Add group osmaxx."""
Group = apps.get_model("auth", "Group")
Group.objects.create(name=settings.OSMAXX_FRONTEND_USER_GROUP)
def update_site_backward(apps, schema_editor):
"""Revert add group osmaxx."""
Group = apps.get_model("auth", "Group")
Group.objects.get(name=settings.OSMAXX_FRONTEND_USER_GROUP).delete()
class Migration(migrations.Migration):
dependencies = [
('auth', '0001_initial'),
]
operations = [
migrations.RunPython(update_site_forward, update_site_backward),
]
此组是在迁移中创建的,因为它应可用于网络应用程序的所有安装。为了让它更有用,我也想给它一个默认权限,所以我把update_site_forward
改成了:
def update_site_forward(apps, schema_editor):
"""Add group osmaxx."""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
ContentType = apps.get_model("contenttypes", "ContentType")
ExtractionOrder = apps.get_model("excerptexport", "ExtractionOrder")
group = Group.objects.create(name=settings.OSMAXX_FRONTEND_USER_GROUP)
content_type = ContentType.objects.get_for_model(ExtractionOrder)
permission = Permission.objects.get(codename='add_extractionorder',
content_type=content_type) # line 16
group.permissions.add(permission)
和Migration.dependencies
到:
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('excerptexport', '0001_initial'),
('auth', '0001_initial'),
]
虽然应用迁移(在第一次还原后)(python3 manage.py migrate auth 0001 && python3 managy.py migrate
)有效,但使用此迁移和所有其他迁移(python3 manage.py migrate
)迁移新创建的 PostgreSQL 数据库失败:
Operations to perform:
Synchronize unmigrated apps: debug_toolbar, django_extensions, messages, humanize, social_auth, kombu_transport_django, staticfiles
Apply all migrations: excerptexport, admin, sites, contenttypes, sessions, default, stored_messages, auth
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying auth.0002_add_default_usergroup_osmaxx...Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/special.py", line 183, in database_forwards
self.code(from_state.apps, schema_editor)
File "/home/osmaxx/source/osmaxx/contrib/auth/migrations/0002_add_default_usergroup_osmaxx.py", line 16, in update_site_forward
permission = Permission.objects.get(codename='add_extractionorder', content_type=content_type)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/query.py", line 334, in get
self.model._meta.object_name
__fake__.DoesNotExist: Permission matching query does not exist.
我做错了什么?
【问题讨论】:
注意:项目的完整代码(没有提到的更改)可在on GitHub 获得。提到的迁移是osmaxx-py/osmaxx/contrib/auth/migrations/0002_add_default_usergroup_osmaxx.py 但请评论,如果问题中未提及的任何内容与回答问题相关,以便我可以将其包含在问题中。 【参考方案1】:默认权限是在post_migrate
信号处理程序中创建的,在迁移运行之后。如果您更新的代码作为第二次 manage.py migrate
运行的一部分运行,这不会成为问题,但在测试套件和任何新部署中都会出现问题。
简单的解决方法是更改此行:
permission = Permission.objects.get(codename='add_extractionorder',
content_type=content_type) # line 16
到这里:
permission, created = Permission.objects.get_or_create(codename='add_extractionorder',
content_type=content_type)
创建默认权限的信号处理程序永远不会创建重复的权限,因此如果它不存在,则创建它是安全的。
【讨论】:
啊,所以权限是在所有迁移之后创建的,而不是在每个人迁移之后创建的? 每次manage.py migrate
命令完成时都会触发post_migrate
信号——所以是的,如果您一次应用所有迁移,它会在所有 个迁移运行后触发。
这个答案有一个解决方法:***.com/questions/31735042/…
也可以迁移到之前的迁移。然后像往常一样继续迁移。这为我解决了。
拯救了我的一天,谢谢!奇怪的是,关于迁移的文档中没有提到这一点。以上是关于Django 迁移失败并显示“__fake__.DoesNotExist:权限匹配查询不存在”。的主要内容,如果未能解决你的问题,请参考以下文章
将 Django 部署到 Elastic Beanstalk,迁移失败
Django:迁移到 NullBooleanField 失败,IntegrityError“包含空值”