Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make sure we only create the minimum number of table indexes for MySQL.

This patch simplifies a bunch of code for all backends and removes some
duplicate index creation for MySQL, in particular (versions 4.x and later).
Patch from Nis Jørgensen.

Fixed #5671, #5680, #7170, #7186.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7790 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f9df4d1435fd3382fdcc06f75c1c8d62bfe78ec6 1 parent b0bc8b9
Malcolm Tredinnick malcolmt authored
1  AUTHORS
@@ -192,6 +192,7 @@ answer newbie questions, and generally made Django that much better:
192 192 james_027@yahoo.com
193 193 jcrasta@gmail.com
194 194 Zak Johnson <zakj@nox.cx>
  195 + Nis Jørgensen <nis@superlativ.dk>
195 196 Michael Josephson <http://www.sdjournal.com/>
196 197 jpellerin@gmail.com
197 198 junzhang.jn@gmail.com
4 django/core/management/commands/createcachetable.py
@@ -21,10 +21,10 @@ def handle_label(self, tablename, **options):
21 21 for f in fields:
22 22 field_output = [qn(f.name), f.db_type()]
23 23 field_output.append("%sNULL" % (not f.null and "NOT " or ""))
24   - if f.unique:
25   - field_output.append("UNIQUE")
26 24 if f.primary_key:
27 25 field_output.append("PRIMARY KEY")
  26 + elif f.unique:
  27 + field_output.append("UNIQUE")
28 28 if f.db_index:
29 29 unique = f.unique and "UNIQUE " or ""
30 30 index_output.append("CREATE %sINDEX %s_%s ON %s (%s);" % \
13 django/core/management/sql.py
@@ -268,11 +268,11 @@ def sql_model_create(model, style, known_models=set()):
268 268 field_output = [style.SQL_FIELD(qn(f.column)),
269 269 style.SQL_COLTYPE(col_type)]
270 270 field_output.append(style.SQL_KEYWORD('%sNULL' % (not f.null and 'NOT ' or '')))
271   - if f.unique and (not f.primary_key or connection.features.allows_unique_and_pk):
272   - field_output.append(style.SQL_KEYWORD('UNIQUE'))
273 271 if f.primary_key:
274 272 field_output.append(style.SQL_KEYWORD('PRIMARY KEY'))
275   - if tablespace and connection.features.supports_tablespaces and (f.unique or f.primary_key) and connection.features.autoindexes_primary_keys:
  273 + elif f.unique:
  274 + field_output.append(style.SQL_KEYWORD('UNIQUE'))
  275 + if tablespace and connection.features.supports_tablespaces and f.unique:
276 276 # We must specify the index tablespace inline, because we
277 277 # won't be generating a CREATE INDEX statement for this field.
278 278 field_output.append(connection.ops.tablespace_sql(tablespace, inline=True))
@@ -355,7 +355,7 @@ def many_to_many_sql_for_model(model, style):
355 355 for f in opts.local_many_to_many:
356 356 if not isinstance(f.rel, generic.GenericRel):
357 357 tablespace = f.db_tablespace or opts.db_tablespace
358   - if tablespace and connection.features.supports_tablespaces and connection.features.autoindexes_primary_keys:
  358 + if tablespace and connection.features.supports_tablespaces:
359 359 tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace, inline=True)
360 360 else:
361 361 tablespace_sql = ''
@@ -460,15 +460,14 @@ def sql_indexes_for_model(model, style):
460 460
461 461 qn = connection.ops.quote_name
462 462 for f in model._meta.local_fields:
463   - if f.db_index and not ((f.primary_key or f.unique) and connection.features.autoindexes_primary_keys):
464   - unique = f.unique and 'UNIQUE ' or ''
  463 + if f.db_index and not f.unique:
465 464 tablespace = f.db_tablespace or model._meta.db_tablespace
466 465 if tablespace and connection.features.supports_tablespaces:
467 466 tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace)
468 467 else:
469 468 tablespace_sql = ''
470 469 output.append(
471   - style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
  470 + style.SQL_KEYWORD('CREATE INDEX') + ' ' + \
472 471 style.SQL_TABLE(qn('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
473 472 style.SQL_KEYWORD('ON') + ' ' + \
474 473 style.SQL_TABLE(qn(model._meta.db_table)) + ' ' + \
2  django/db/backends/__init__.py
@@ -41,8 +41,6 @@ def make_debug_cursor(self, cursor):
41 41
42 42 class BaseDatabaseFeatures(object):
43 43 allows_group_by_ordinal = True
44   - allows_unique_and_pk = True
45   - autoindexes_primary_keys = True
46 44 inline_fk_references = True
47 45 needs_datetime_string_cast = True
48 46 supports_constraints = True
1  django/db/backends/mysql/base.py
@@ -60,7 +60,6 @@
60 60 # TRADITIONAL will automatically cause most warnings to be treated as errors.
61 61
62 62 class DatabaseFeatures(BaseDatabaseFeatures):
63   - autoindexes_primary_keys = False
64 63 inline_fk_references = False
65 64 empty_fetchmany_value = ()
66 65 update_can_self_select = False
1  django/db/backends/mysql_old/base.py
@@ -64,7 +64,6 @@ def __getattr__(self, attr):
64 64 return getattr(self.cursor, attr)
65 65
66 66 class DatabaseFeatures(BaseDatabaseFeatures):
67   - autoindexes_primary_keys = False
68 67 inline_fk_references = False
69 68 empty_fetchmany_value = ()
70 69 update_can_self_select = False
1  django/db/backends/oracle/base.py
@@ -24,7 +24,6 @@
24 24
25 25 class DatabaseFeatures(BaseDatabaseFeatures):
26 26 allows_group_by_ordinal = False
27   - allows_unique_and_pk = False # Suppress UNIQUE/PK for Oracle (ORA-02259)
28 27 empty_fetchmany_value = ()
29 28 needs_datetime_string_cast = False
30 29 supports_tablespaces = True
6 django/db/models/fields/__init__.py
@@ -91,7 +91,7 @@ def __init__(self, verbose_name=None, name=None, primary_key=False,
91 91 self.name = name
92 92 self.verbose_name = verbose_name
93 93 self.primary_key = primary_key
94   - self.max_length, self.unique = max_length, unique
  94 + self.max_length, self._unique = max_length, unique
95 95 self.blank, self.null = blank, null
96 96 # Oracle treats the empty string ('') as null, so coerce the null
97 97 # option whenever '' is a possible value.
@@ -168,6 +168,10 @@ def db_type(self):
168 168 except KeyError:
169 169 return None
170 170
  171 + def unique(self):
  172 + return self._unique or self.primary_key
  173 + unique = property(unique)
  174 +
171 175 def validate_full(self, field_data, all_data):
172 176 """
173 177 Returns a list of errors for this field. This is the main interface,

0 comments on commit f9df4d1

Please sign in to comment.
Something went wrong with that request. Please try again.