You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
908 B
22 lines
908 B
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.db import connection
|
|
from lms.settings import apps
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Set seq by last id in table.'
|
|
|
|
def handle(self, **_):
|
|
with connection.cursor() as cursor:
|
|
for i in ContentType.objects.all():
|
|
if i.app_label in apps:
|
|
model_name = "%s_%s" % (i.app_label, i.model)
|
|
cursor.execute('BEGIN')
|
|
cursor.execute('LOCK TABLE %s IN EXCLUSIVE MODE' % model_name)
|
|
cursor.execute(
|
|
"""SELECT setval('%s_id_seq', COALESCE(
|
|
(SELECT MAX(id)+1 FROM %s), 1) ,false
|
|
)""" % (model_name, model_name)
|
|
)
|
|
cursor.execute('COMMIT') |