def index_name(model, *field_names, **kwargs):
if not len(field_names):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>using = kwargs.pop('<STR_LIT>', DEFAULT_DB_ALIAS)<EOL>if len(kwargs):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>existing_fields = {field.name: field for field in model._meta.fields}<EOL>fields = [existing_fields[name] for name in field_names<EOL>if name in existing_fields]<EOL>if len(fields) != len(field_names):<EOL><INDENT>unfound_names = set(field_names) - {field.name for field in fields}<EOL>raise ValueError("<STR_LIT>" + "<STR_LIT:U+002C>".join(unfound_names))<EOL><DEDENT>column_names = tuple(field.column for field in fields)<EOL>list_sql = get_list_sql(column_names)<EOL>with connections[using].cursor() as cursor:<EOL><INDENT>cursor.execute(<EOL>"""<STR_LIT>""".format(list_sql=list_sql),<EOL>(model._meta.db_table,) + column_names,<EOL>)<EOL>indexes = defaultdict(list)<EOL>for index_name, _, column_name in cursor.fetchall():<EOL><INDENT>indexes[index_name].append(column_name)<EOL><DEDENT><DEDENT>indexes_by_columns = {tuple(v): k for k, v in indexes.items()}<EOL>try:<EOL><INDENT>return indexes_by_columns[column_names]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise KeyError("<STR_LIT>" + "<STR_LIT:U+002C>".join(field_names) + "<STR_LIT:)>")<EOL><DEDENT>
Returns the name of the index existing on field_names, or raises KeyError
if no such index exists.