gestioCOF/utils/models.py
Aurélien Delobelle 09a96a0375 Fix postgres sequences
When manually assigning a value to AutoField, sequences must be updated
manually.

https://docs.djangoproject.com/en/1.11/ref/databases/#manually-specifying-values-of-auto-incrementing-primary-keys
2017-10-28 17:30:37 +02:00

17 lines
604 B
Python

from django.core.management.color import no_style
from django.db import connection
def sqlsequencereset(models, conn=connection):
"""
Update sequences of fields of `models`.
This function should be called after manually assigning a value to an
`AutoField`.
https://docs.djangoproject.com/en/1.11/ref/databases/#manually-specifying-values-of-auto-incrementing-primary-keys
"""
sequence_sql = conn.ops.sequence_reset_sql(no_style(), models)
if sequence_sql:
with conn.cursor() as cursor:
for line in sequence_sql:
cursor.execute(line)