Django

Code

Ticket #1286: mysql.diff

File mysql.diff, 3.3 kB (added by Jan Rademaker <j.rademaker@gmail.com>, 3 years ago)

Covers additional scenarios

  • django/core/db/backends/mysql.py

    old new  
    137137 
    138138def get_indexes(cursor, table_name): 
    139139    """ 
    140     Returns a dictionary of fieldname -> infodict for the given table, 
     140    Returns a dictionary of key_name -> infodict for the given table, 
    141141    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} 
    144144    """ 
    145145    cursor.execute("SHOW INDEX FROM %s" % DatabaseWrapper().quote_name(table_name)) 
    146146    indexes = {} 
    147147    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) 
    149157    return indexes 
    150158 
    151159OPERATOR_MAPPING = { 
  • django/core/management.py

    old new  
    639639 
    640640                # Add primary_key and unique, if necessary. 
    641641                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 
    647655 
    648656                field_type += '(' 
    649657 
     
    663671            yield '    %s' % field_desc 
    664672        yield '    class META:' 
    665673        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 
    666678        yield '' 
    667679inspectdb.help_doc = "Introspects the database tables in the given database and outputs a Django model module." 
    668680inspectdb.args = "[dbname]"