If you’ve ever encountered the following error during Laravel migrations, especially when working with MySQL or MariaDB, you’re not alone: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes This error is common, but the good news is — it’s easily fixable. Let’s explore why this […]
If you’ve ever encountered the following error during Laravel migrations, especially when working with MySQL or MariaDB, you’re not alone:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
This error is common, but the good news is — it’s easily fixable. Let's explore why this happens, how to resolve it, and what settings you should check to avoid it in the future.
This issue originates from MySQL, not Laravel itself.
By default, Laravel uses the InnoDB storage engine and the utf8mb4 character set for better Unicode support (like emojis). But older MySQL/MariaDB versions and misconfigured MySQL setups may still use:
Compact
, instead of Dynamic
All of these can trigger the 1071 error when indexing long string columns.
If your MySQL is still using MyISAM
as the default engine, switch to InnoDB
. Here's how:
my.ini
or my.cnf
Open your MySQL configuration file:
/etc/mysql/my.cnf
or /etc/mysql/mysql.conf.d/mysqld.cnf
C:\ProgramData\MySQL\MySQL Server x.x\my.ini
Under the [mysqld]
section, add or update:
default-storage-engine = InnoDB
Save and restart your MySQL server.
You can explicitly define the storage engine in your Laravel project inside config/database.php
:
'mysql' => [
// other config...
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
],
This helps Laravel apply the correct row format and engine when creating tables.
Older MySQL setups default to ROW_FORMAT=Compact
, which can conflict with long index keys.
To set the row format to dynamic, do one of the following:
Add this in your my.cnf
or my.ini
:
innodb_default_row_format = dynamic
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
Schema::defaultStringLength(191)
(Fallback Option)In some cases, when none of the above work (usually on MySQL < 5.7.7 or MariaDB < 10.2.2), you can fall back to:
use Illuminate\Database\Schema\Builder;
public function boot(): void
{
Builder::defaultStringLength(191);
}
But remember — this is a workaround, not a real fix. Laravel includes this to support legacy systems, but ideally, your MySQL settings should be compatible with Laravel's default behavior.
No. This issue is entirely on the MySQL configuration side. Laravel expects a modern database setup (InnoDB, utf8mb4, dynamic row format). If your MySQL isn’t configured accordingly, you might face this error.
That’s why Laravel doesn’t "fix" this by default in each new project. It’s your environment’s responsibility to meet modern standards.
Problem | Solution |
---|---|
MyISAM engine | Change to InnoDB in my.ini |
Row format compact | Use ROW_FORMAT=DYNAMIC |
Key length > 1000 bytes | Shorten string length or upgrade MySQL |
Still facing issues? | Use Schema::defaultStringLength(191) as fallback |
Fixing the "Specified key was too long" error in Laravel is mostly about setting up your MySQL environment correctly. Once you've updated the default engine and row format, you’ll rarely see this issue again.
Happy coding! 🚀
Feel free to bookmark this guide for the next time you hit a migration error!