You're on a modern MySQL 8, so the old "767 bytes" problem shouldn't apply any more. Then a migration fails with this:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;
max key length is 3072 bytes (Connection: mysql, SQL: alter table `links`
add unique `links_url_unique`(`url`))
This is the same error with a bigger number. The usual fix for the 767-byte version, Schema::defaultStringLength(191), isn't the right tool here. This guide explains where 3072 comes from, how to tell which index is too big, and the fixes that actually work.
Where 3072 bytes comes from
InnoDB limits how large one index entry can be. From the MySQL manual:
- 3072 bytes for tables using the
DYNAMICorCOMPRESSEDrow format, which is the default on MySQL 5.7 and 8.x. - 767 bytes for the older
REDUNDANTandCOMPACTformats. That's the limit behind the classic error, covered in our 767-byte guide.
The limit is counted in bytes, but columns are sized in characters. With utf8mb4, Laravel's default character set, MySQL reserves 4 bytes per character, so:
| Column | Index size with utf8mb4 |
|---|---|
VARCHAR(191) | 764 bytes |
VARCHAR(255), Laravel's string() | 1,020 bytes |
VARCHAR(768) | 3,072 bytes, the most a single indexed column can be |
VARCHAR(1000) | 4,000 bytes: too long |
The two ways to hit it
1. One long column with an index
The most common case: a URL, a path or a long external ID with a unique index.
$table->string('url', 2048)->unique(); // 8,192 bytes: fails
2. A composite index over several strings
The sizes of all columns in one index add up. Each string() is 1,020 bytes, so a unique key over four of them is 4,080 bytes:
$table->unique(['provider', 'account', 'region', 'name']); // 4 × 1,020: fails
Three of them (3,060 bytes) would just fit. Add one integer column and it can tip over.
Step 1: find the index that's too big
The error shows the SQL statement that failed, which names the index. Add up the columns in it: for each VARCHAR(n) or CHAR(n) column, count n × 4 bytes with utf8mb4; integers are 4 or 8 bytes; dates are a few bytes. Anything over 3,072 has to change.
Step 2: pick a fix
Fix A: make the columns as long as they need to be, not 255 by habit
Often the simplest answer is that the columns don't need 255 characters. A region code fits in 32, a provider name in 50. Size the columns honestly:
$table->string('provider', 50);
$table->string('region', 32);
$table->string('account', 191);
$table->string('name', 191);
$table->unique(['provider', 'account', 'region', 'name']); // (50+191+32+191) × 4 = 1,856 bytes
Validate the same lengths in your form requests, or you'll trade this error for "1406 Data too long for column".
Fix B: index a hash instead of the long value
For values that really are long, such as URLs, store the value in a column without an index and put a unique index on a fixed-length hash of it. A SHA-256 hash is always 64 hex characters, and those characters are plain ASCII, so the index is 64 bytes.
MySQL can keep the hash up to date for you with a generated column:
Schema::create('links', function (Blueprint $table) {
$table->id();
$table->text('url');
$table->char('url_hash', 64)->charset('ascii')->collation('ascii_bin')
->storedAs('SHA2(`url`, 256)')
->unique();
$table->timestamps();
});
Look rows up by the hash, which uses the index:
$link = Link::where('url_hash', hash('sha256', $url))->first();
PHP's hash('sha256', ...) and MySQL's SHA2(..., 256) produce the same lowercase hex string for the same UTF-8 text. The one difference from a unique index on the value itself: https://Example.com and https://example.com are different strings, so they get different hashes. Normalise values before saving if case or trailing slashes shouldn't count as different.
Fix C: a prefix index, for lookups only
If you only need the index to speed up searches, not to enforce uniqueness, MySQL can index the first part of a column. Laravel's schema builder doesn't support prefix lengths, so use a raw statement in the migration:
DB::statement('CREATE INDEX links_url_prefix ON links (url(255))');
Don't use a prefix for a unique index: MySQL would then treat two URLs that share their first 255 characters as duplicates.
Why not just use defaultStringLength(191)?
Schema::defaultStringLength(191) shortens every string() column in every future migration, whether it's indexed or not. It was a workaround for the 767-byte limit on old MySQL versions. With a 3072-byte limit it doesn't solve the long-URL case, it hides the composite case only by accident, and it silently caps columns that never needed capping. Fix the specific index instead.
Why not change the character set?
Switching a table to utf8mb3 (3 bytes per character) or latin1 makes the index fit, but those can't store emoji and many scripts. You'd swap a migration error for broken text later. Keep utf8mb4 for anything users type. The one safe case is a column that only ever holds ASCII, like the hash above, codes or IDs; there ->charset('ascii') is the right choice anyway.
FAQ
Why did this work on my machine?
Probably because your local database is SQLite or PostgreSQL, which have different index limits, or a MySQL whose table uses a different character set. Run migrations against the same database engine you use in production.
The migration failed halfway. What now?
MySQL can't roll back schema changes, so the table may already exist without the index, while Laravel hasn't recorded the migration as run. Fix the migration, drop the half-created table by hand (on a fresh table with no data), and run php artisan migrate again.
Does PostgreSQL have the same limit?
PostgreSQL has a limit of its own, roughly 2.7 KB per B-tree index entry, with a different error message ("index row size exceeds"). The hash approach works there too.
Bottom line
"Max key length is 3072 bytes" means one index is too big: with utf8mb4 each character counts as 4 bytes, so a single indexed column can be at most 768 characters, and composite indexes add up their columns. Size columns to what they really hold, index a SHA-256 hash for long values like URLs, and use a prefix index only for non-unique lookups.
Limits checked on 2 November 2026 against the MySQL 8.4 reference manual (InnoDB limits) and the Laravel 13 schema builder documentation.
Comments 0
Be the first to comment.
Leave a comment