Updated June 2026. Tested on Laravel 13 and PHP 8.4.
If you have hit this error while running migrations, here is the quick fix and the reason behind it.
SQLSTATE[42000]: Syntax error or access violation: 1071
Specified key was too long; max key length is 767 bytes
(SQL: alter table `users` add unique `users_email_unique`(`email`))
Why it happens
Laravel uses the utf8mb4 character set by default, which supports the full range of Unicode including emojis. In utf8mb4, each character can take up to four bytes, so a 255 character VARCHAR with a unique index needs 1020 bytes. Older databases cap an index key at 767 bytes, and the index blows past that.
This only bites you on MySQL older than 5.7.7 or MariaDB. On modern MySQL 8, the limit is much higher and you will never see this error.
The fix
Tell Laravel to use a shorter default string length, 191 characters, which keeps a utf8mb4 indexed column inside the 767 byte limit. Set it once in the boot method of App\Providers\AppServiceProvider.
use Illuminate\Support\Facades\Schema;
public function boot(): void
{
Schema::defaultStringLength(191);
}
Now drop your tables and migrate again, and the indexes fit.
php artisan migrate:fresh
The better fix if you can
Setting 191 everywhere is a workaround that shortens every string column. If you are able to, the cleaner answer is to run a current database. MySQL 8 does not have this limit, so you can leave the default 255 and never think about it. Use defaultStringLength(191) when you are stuck on an older MySQL or MariaDB, and otherwise just upgrade the database. Questions welcome in the comments.
All comments ()
No comments yet
Be the first to leave a comment on this post.