Re: [Django] #34165: migrate management command does not respect database parameter when adding Permissions. (was: migrate management command does not respect database parameter for all DB calls)

3 views
Skip to first unread message

Django

unread,
Nov 16, 2022, 8:24:05 AM11/16/22
to django-...@googlegroups.com
#34165: migrate management command does not respect database parameter when adding
Permissions.
------------------------------+------------------------------------
Reporter: Vasanth | Owner: nobody
Type: Bug | Status: new
Component: contrib.auth | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* component: Migrations => contrib.auth
* stage: Unreviewed => Accepted


Comment:

Thanks for this report, it's related with adding missing permissions. I
was able to fix this by setting `_state.db`, however I'm not convinced
that it's the best solution:
{{{#!diff
diff --git a/django/contrib/auth/management/__init__.py
b/django/contrib/auth/management/__init__.py
index 0b5a982617..27fe0df1d7 100644
--- a/django/contrib/auth/management/__init__.py
+++ b/django/contrib/auth/management/__init__.py
@@ -94,12 +94,15 @@ def create_permissions(
)
.values_list("content_type", "codename")
)
-
- perms = [
- Permission(codename=codename, name=name, content_type=ct)
- for ct, (codename, name) in searched_perms
- if (ct.pk, codename) not in all_perms
- ]
+ perms = []
+ for ct, (codename, name) in searched_perms:
+ if (ct.pk, codename) not in all_perms:
+ permission = Permission()
+ permission._state.db = using
+ permission.codename = codename
+ permission.name = name
+ permission.content_type = ct
+ perms.append(permission)
Permission.objects.using(using).bulk_create(perms)
if verbosity >= 2:
for perm in perms:

}}}

Partly related to #29843.

--
Ticket URL: <https://code.djangoproject.com/ticket/34165#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages