Restart the AuthGroup id sequence on postgres

This commit is contained in:
FinnStutzenstein 2019-04-02 07:50:09 +02:00
parent 55c162809c
commit 018df697fa
1 changed files with 10 additions and 0 deletions

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};")