Menu

Fixing SQLSTATE[42000]: 1071 Specified Key Was Too Long (1000 & 767 Bytes) in Laravel
πŸ“°

Fixing SQLSTATE[42000]: 1071 Specified Key Was Too Long (1000 & 767 Bytes) in Laravel

DEV Community: laravelΒ·Chathura RathnayakaΒ·7 days ago
#8RGdkyoa
#devtolaravel#devto#dev#table#bytes
Reading 0:00
15s threshold

You configure your database credentials, run php artisan migrate to build your tables, and the migration crashes halfway through the users or jobs table with this terminal error:

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (Connection: mysql, SQL: alter table `users` add unique `users_email_unique`(`email`))

Enter fullscreen mode

Exit fullscreen mode

Depending on your MySQL or MariaDB version, the error message will state a max key length of 1000 bytes , 767 bytes , or 3072 bytes .

The Math Behind the Crash

By default, Laravel uses the utf8mb4 character set so your database can store full Unicode characters and emojis. Unlike older utf8 (which uses 3 bytes per character), utf8mb4 reserves up to 4 bytes per character .

When a migration creates a standard string column ( $table->string('email')->unique() ), Laravel sets it to VARCHAR(255) .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.