Code

Ticket #1286: mysql.diff

File mysql.diff, 3.3 KB (added by Jan Rademaker <j.rademaker@…>, 8 years ago)

Covers additional scenarios

Line 
1Index: django/core/db/backends/mysql.py
2===================================================================
3--- django/core/db/backends/mysql.py    (revision 2357)
4+++ django/core/db/backends/mysql.py    (working copy)
5@@ -137,15 +137,23 @@
6 
7 def get_indexes(cursor, table_name):
8     """
9-    Returns a dictionary of fieldname -> infodict for the given table,
10+    Returns a dictionary of key_name -> infodict for the given table,
11     where each infodict is in the format:
12-        {'primary_key': boolean representing whether it's the primary key,
13-         'unique': boolean representing whether it's a unique index}
14+        {'unique': boolean representing whether it's a unique index,
15+         'columns': list of columns in the index}
16     """
17     cursor.execute("SHOW INDEX FROM %s" % DatabaseWrapper().quote_name(table_name))
18     indexes = {}
19     for row in cursor.fetchall():
20-        indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
21+        key_name = row[2]
22+        column_name = row[4]
23+        unique = not bool(row[1])
24+        if not indexes.has_key(key_name):
25+            indexes[key_name] = {
26+                'unique': unique,
27+                'columns': []
28+            }
29+        indexes[key_name]['columns'].append(column_name)
30     return indexes
31 
32 OPERATOR_MAPPING = {
33Index: django/core/management.py
34===================================================================
35--- django/core/management.py   (revision 2357)
36+++ django/core/management.py   (working copy)
37@@ -639,11 +639,19 @@
38 
39                 # Add primary_key and unique, if necessary.
40                 column_name = extra_params.get('db_column', att_name)
41-                if column_name in indexes:
42-                    if indexes[column_name]['primary_key']:
43-                        extra_params['primary_key'] = True
44-                    elif indexes[column_name]['unique']:
45-                        extra_params['unique'] = True
46+                # Check if this field is the sole primary key.
47+                if indexes.has_key('PRIMARY') and \
48+                   column_name in indexes['PRIMARY']['columns'] and \
49+                   len(indexes['PRIMARY']['columns']) == 1:
50+                    extra_params['primary_key'] = True
51+                else:
52+                    # Check if this field is part of any other unique index.
53+                    # Don't assume this field to be unique when the index
54+                    # spans several columns.
55+                    for index_name in indexes:
56+                        if column_name in indexes[index_name]['columns'] and \
57+                           len(indexes[index_name]['columns']) == 1:
58+                            extra_params['unique'] = True
59 
60                 field_type += '('
61 
62@@ -663,6 +671,10 @@
63             yield '    %s' % field_desc
64         yield '    class META:'
65         yield '        db_table = %r' % table_name
66+        for index_name in indexes:
67+            if indexes[index_name]['unique'] and len(indexes[index_name]['columns']) > 1:
68+                yield '        unique_together = ((%s),)' % ', '.join(['"%s"' % column_name for column_name in indexes[index_name]['columns']])
69+                break
70         yield ''
71 inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module."
72 inspectdb.args = "[dbname]"