Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AspieArchitect's avatar

Maximum object name length in MySQL

When creating a unique index the index name will be in the format tablename_fieldname1_fieldname2....fieldnamex_unique

If that name exceeds the 64 character limit in MySQL then the migration will fail as an exception is thrown by MySQL.

I have used the DB:unprepared() statement to generate the unique index I want and this is an acceptable work-around.

Ideally I'd like to change the createIndexName() function in the \illuminate\database\schema\blueprint.php file to detect overlong names and generate something more sensible.

If I were to do this then that would be fine for MySQL but for ANSI compliant databases the limit is 128 characters rather than 64.

Is there a way for the blueprint.php file to detect which grammar or database platform being used and therefore the appropriate maximum field length?

0 likes
2 replies
jlrdw's avatar

Just curious why do you require such long names?

1 like
AspieArchitect's avatar

None of the names are particularly long but the combination of names is.

I've inherited the database with quite a bit of technical debt quite close to the deployment date so am constrained on making amendments.

The naming convention used used by the framework is as I would approach it as a DBA. Maybe I would shorten _unique to uq or foreign to fk thought to be honest the "foreign" and "unique" monikers are for human readability rather than mechanically necessary.

In this case it is possible to have a maximum object name of 64 characters long therefore any code generating objects combining names has to allow for the possibility that there will be combinations over 64 characters long. There must be a named law for this observation somewhere.

One observation I would make is that if a database field allows up to 'n' characters then sooner or later someone is going to put 'n' characters in it. Same thing applies to unbounded dates and ages. Sooner or later you are going to find Methuselah's mum or Dr Who's holiday snaps.

Please or to participate in this conversation.