Django

Code

Changeset 5455

Show
Ignore:
Timestamp:
06/09/07 22:31:26 (2 years ago)
Author:
mtredinnick
Message:

Fixed #4432 -- Fixed PostgreSQL sequence resetting in the case when a table has
no rows yet. Thanks, mrmachine.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r5453 r5455  
    171171    mmarshall 
    172172    Eric Moritz <http://eric.themoritzfamily.com/> 
     173    mrmachine <real.human@mrmachine.net> 
    173174    Robin Munn <http://www.geekforgod.com/> 
    174175    Robert Myers <myer0052@gmail.com> 
  • django/trunk/django/db/backends/postgresql/base.py

    r5302 r5455  
    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()))) 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5204 r5455  
    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())))