Merge pull request #4556 from FinnStutzenstein/postgresAuthGroupSequence

Restart the AuthGroup id sequence on postgres
This commit is contained in:
Emanuel Schütze 2019-04-03 21:16:42 +02:00 committed by GitHub
commit 58e0e4155e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
from django.apps import apps
from django.contrib.auth.models import Permission
from django.db import connection
from django.db.models import Q
from ..utils.auth import GROUP_ADMIN_PK, GROUP_DEFAULT_PK
@ -185,3 +186,12 @@ def create_builtin_groups_and_admin(**kwargs):
# After each group was created, the permissions (many to many fields) where
# added to the group. But we do not have to update the cache by calling
# inform_changed_data() because the cache is updated on server start.
# For postgres, the id sequence (the current auto increment value for the id field)
# needs to be refreshed after inserting the groups per id, because postgres does not
# increment the sequence then.
if connection.vendor == "postgresql":
with connection.cursor() as cursor:
cursor.execute("SELECT max(id) + 1 as max FROM auth_group;")
max_id = cursor.fetchone()[0]
cursor.execute(f"ALTER SEQUENCE auth_group_id_seq RESTART WITH {max_id};")