﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31881	Raise Index name limit for postgres	Jarek Glowacki	nobody	"Hi!

This line here: https://docs.djangoproject.com/en/3.1/ref/models/indexes/#django.db.models.Index.name


>**Index.name**
>
>... For compatibility with different databases, index names cannot be longer than 30 characters and shouldn’t start with a number (0-9) or underscore (_).

Okay, but why is this restriction maintained when using a specific backend's db index class, namely `PostgresIndex`?
https://github.com/django/django/blob/8c7992f658e1125f8dc20b19206e8bbbe4187372/django/contrib/postgres/indexes.py#L11-L19

This issue has become more noticeable with django 3.0, now that interpolated names are allowed for index definitions. Nice feature, until it falls over because one of your models has a longer name than the rest. :(

I'd like to propose we change the docs to read:
>**Index.name**
>
>... For compatibility with different databases, index names cannot be longer than 30 characters and shouldn’t start with a number (0-9) or underscore (_). To get around this restriction, you can directly import and use the Index class for your database backend (eg `PostgresIndex`), or write your own.

And change the code to just:
{{{
class PostgresIndex(Index):

    @cached_property
    def max_name_length(self):
        return 63

    ...
}}}

Notes:
- This _should_ be backwards compatible, though I'm not entirely sure how django creates indexes automatically and applies name truncation in these cases, if it were to somewhy use this class, we'd start getting different truncation to what we had in the past and the user would need to make migrations to recreate some of their indexes with new names.. surely not?
- I'm not sure whether this change should be limited to indexes only. I had a brief look and found [https://github.com/django/django/blob/18d4eac7fce6dfab6149ec7aba72f3c765b5722c/django/db/backends/postgresql/operations.py#L193-L204 / this], which already does what I'm suggesting elsewhere. We should pull the 63 out into a `POSTGRES_IDENTIFIER_MAX_NAME_LENGTH` constant and apply it more broadly?

Given the goahead, I'm happy to turn the above into a PR.

Thanks for your time. :)
"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	needsinfo	postgres index max_name_length	Florian Demmer	Unreviewed	0	0	0	0	0	0
