Ticket #4432: django-4432.2.diff

File django-4432.2.diff, 4.2 KB (added by real.human@…, 8 years ago)

Reset sequence to the max pk value if there are records, otherwise reset it to 1.

  • django/db/backends/postgresql/base.py

     
    219219    from django.db import models
    220220    output = []
    221221    for model in model_list:
     222        # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
     223        # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
     224        # if there are records (as the max pk value is already in use), otherwise set it to false.
    222225        for f in model._meta.fields:
    223226            if isinstance(f, models.AutoField):
    224                 output.append("%s setval('%s', (%s max(%s) %s %s));" % \
     227                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    225228                    (style.SQL_KEYWORD('SELECT'),
    226229                    style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
    227                     style.SQL_KEYWORD('SELECT'),
    228230                    style.SQL_FIELD(quote_name(f.column)),
     231                    style.SQL_FIELD(quote_name(f.column)),
     232                    style.SQL_KEYWORD('IS NOT'),
    229233                    style.SQL_KEYWORD('FROM'),
    230234                    style.SQL_TABLE(quote_name(model._meta.db_table))))
    231235                break # Only one AutoField is allowed per model, so don't bother continuing.
    232236        for f in model._meta.many_to_many:
    233             output.append("%s setval('%s', (%s max(%s) %s %s));" % \
     237            output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    234238                (style.SQL_KEYWORD('SELECT'),
    235239                style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
    236                 style.SQL_KEYWORD('SELECT'),
    237240                style.SQL_FIELD(quote_name('id')),
     241                style.SQL_FIELD(quote_name('id')),
     242                style.SQL_KEYWORD('IS NOT'),
    238243                style.SQL_KEYWORD('FROM'),
    239244                style.SQL_TABLE(f.m2m_db_table())))
    240245    return output
  • django/db/backends/postgresql_psycopg2/base.py

     
    176176    from django.db import models
    177177    output = []
    178178    for model in model_list:
     179        # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
     180        # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
     181        # if there are records (as the max pk value is already in use), otherwise set it to false.
    179182        for f in model._meta.fields:
    180183            if isinstance(f, models.AutoField):
    181                 output.append("%s setval('%s', (%s max(%s) %s %s));" % \
     184                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    182185                    (style.SQL_KEYWORD('SELECT'),
    183186                    style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))),
    184                     style.SQL_KEYWORD('SELECT'),
    185187                    style.SQL_FIELD(quote_name(f.column)),
     188                    style.SQL_FIELD(quote_name(f.column)),
     189                    style.SQL_KEYWORD('IS NOT'),
    186190                    style.SQL_KEYWORD('FROM'),
    187191                    style.SQL_TABLE(quote_name(model._meta.db_table))))
    188192                break # Only one AutoField is allowed per model, so don't bother continuing.
    189193        for f in model._meta.many_to_many:
    190             output.append("%s setval('%s', (%s max(%s) %s %s));" % \
     194            output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    191195                (style.SQL_KEYWORD('SELECT'),
    192196                style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())),
    193                 style.SQL_KEYWORD('SELECT'),
    194197                style.SQL_FIELD(quote_name('id')),
     198                style.SQL_FIELD(quote_name('id')),
     199                style.SQL_KEYWORD('IS NOT'),
    195200                style.SQL_KEYWORD('FROM'),
    196201                style.SQL_TABLE(f.m2m_db_table())))
    197202    return output
Back to Top