Ticket #1286: mysql.diff
File mysql.diff, 3.3 KB (added by , 19 years ago) |
---|
-
django/core/db/backends/mysql.py
137 137 138 138 def get_indexes(cursor, table_name): 139 139 """ 140 Returns a dictionary of fieldname -> infodict for the given table,140 Returns a dictionary of key_name -> infodict for the given table, 141 141 where each infodict is in the format: 142 {' primary_key': boolean representing whether it's the primary key,143 ' unique': boolean representing whether it's a unique index}142 {'unique': boolean representing whether it's a unique index, 143 'columns': list of columns in the index} 144 144 """ 145 145 cursor.execute("SHOW INDEX FROM %s" % DatabaseWrapper().quote_name(table_name)) 146 146 indexes = {} 147 147 for row in cursor.fetchall(): 148 indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])} 148 key_name = row[2] 149 column_name = row[4] 150 unique = not bool(row[1]) 151 if not indexes.has_key(key_name): 152 indexes[key_name] = { 153 'unique': unique, 154 'columns': [] 155 } 156 indexes[key_name]['columns'].append(column_name) 149 157 return indexes 150 158 151 159 OPERATOR_MAPPING = { -
django/core/management.py
639 639 640 640 # Add primary_key and unique, if necessary. 641 641 column_name = extra_params.get('db_column', att_name) 642 if column_name in indexes: 643 if indexes[column_name]['primary_key']: 644 extra_params['primary_key'] = True 645 elif indexes[column_name]['unique']: 646 extra_params['unique'] = True 642 # Check if this field is the sole primary key. 643 if indexes.has_key('PRIMARY') and \ 644 column_name in indexes['PRIMARY']['columns'] and \ 645 len(indexes['PRIMARY']['columns']) == 1: 646 extra_params['primary_key'] = True 647 else: 648 # Check if this field is part of any other unique index. 649 # Don't assume this field to be unique when the index 650 # spans several columns. 651 for index_name in indexes: 652 if column_name in indexes[index_name]['columns'] and \ 653 len(indexes[index_name]['columns']) == 1: 654 extra_params['unique'] = True 647 655 648 656 field_type += '(' 649 657 … … 663 671 yield ' %s' % field_desc 664 672 yield ' class META:' 665 673 yield ' db_table = %r' % table_name 674 for index_name in indexes: 675 if indexes[index_name]['unique'] and len(indexes[index_name]['columns']) > 1: 676 yield ' unique_together = ((%s),)' % ', '.join(['"%s"' % column_name for column_name in indexes[index_name]['columns']]) 677 break 666 678 yield '' 667 679 inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module." 668 680 inspectdb.args = "[dbname]"