Ticket #4432: django-4432.2.diff
File django-4432.2.diff, 4.2 KB (added by , 17 years ago) |
---|
-
django/db/backends/postgresql/base.py
219 219 from django.db import models 220 220 output = [] 221 221 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. 222 225 for f in model._meta.fields: 223 226 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;" % \ 225 228 (style.SQL_KEYWORD('SELECT'), 226 229 style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 227 style.SQL_KEYWORD('SELECT'),228 230 style.SQL_FIELD(quote_name(f.column)), 231 style.SQL_FIELD(quote_name(f.column)), 232 style.SQL_KEYWORD('IS NOT'), 229 233 style.SQL_KEYWORD('FROM'), 230 234 style.SQL_TABLE(quote_name(model._meta.db_table)))) 231 235 break # Only one AutoField is allowed per model, so don't bother continuing. 232 236 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;" % \ 234 238 (style.SQL_KEYWORD('SELECT'), 235 239 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 236 style.SQL_KEYWORD('SELECT'),237 240 style.SQL_FIELD(quote_name('id')), 241 style.SQL_FIELD(quote_name('id')), 242 style.SQL_KEYWORD('IS NOT'), 238 243 style.SQL_KEYWORD('FROM'), 239 244 style.SQL_TABLE(f.m2m_db_table()))) 240 245 return output -
django/db/backends/postgresql_psycopg2/base.py
176 176 from django.db import models 177 177 output = [] 178 178 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. 179 182 for f in model._meta.fields: 180 183 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;" % \ 182 185 (style.SQL_KEYWORD('SELECT'), 183 186 style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 184 style.SQL_KEYWORD('SELECT'),185 187 style.SQL_FIELD(quote_name(f.column)), 188 style.SQL_FIELD(quote_name(f.column)), 189 style.SQL_KEYWORD('IS NOT'), 186 190 style.SQL_KEYWORD('FROM'), 187 191 style.SQL_TABLE(quote_name(model._meta.db_table)))) 188 192 break # Only one AutoField is allowed per model, so don't bother continuing. 189 193 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;" % \ 191 195 (style.SQL_KEYWORD('SELECT'), 192 196 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 193 style.SQL_KEYWORD('SELECT'),194 197 style.SQL_FIELD(quote_name('id')), 198 style.SQL_FIELD(quote_name('id')), 199 style.SQL_KEYWORD('IS NOT'), 195 200 style.SQL_KEYWORD('FROM'), 196 201 style.SQL_TABLE(f.m2m_db_table()))) 197 202 return output