Migration index too long? Choose the name yourself!

Migrations are a great way of building database schema, but sometimes it’s harder to deal with more than just columns. One of more interesting things are indexes. In particular, I had a problem of auto-assigning a name to unique index, which appeared to be too long. What to do with it?

A specific example – let’s say you want to set up a unique index on four columns:

With that you can run into this error:

The thing is that Laravel is trying to automatically assign a name to the index, and MySQL has a limit here: 64 characters for identifier name.

Solution is to specify your own name for the structure. If you read official laravel documentation, it’s not mentioned, the examples are pretty simple:


But if you dig deeper, then the exact function unique() is defined like this:

Which means that you can specify your own name for the index.

Notice: Just make sure it’s not longer than 64 characters and is not among MySQL reserved words.

Notice 2: Same rule applies to other index identifiers – functions primary() or just index() -you can specify the names as well, instead of allowing Laravel to auto-assign the names.

Liked the article? Maybe you need help with your Laravel project?
Check out my Upwork profile and hire me!

2 thoughts on “Migration index too long? Choose the name yourself!

Leave a Reply

Your email address will not be published. Required fields are marked *