Home / Snippets / How to Prevent Key Is Too Long Error in Laravel
How to Prevent Key Is Too Long Error in Laravel cover

How to Prevent Key Is Too Long Error in Laravel


2 years ago


Sometimes you might come across an error saying 'specified key is too long" when defining a migration in Laravel and there are several ways to fix this issue.
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
The cause of the error above is because of a number that's higher than 255 characters so by specifying the code below you will get the error message as mentioned above.
<?php $table->string('email', 300);

Method 1: Specify Smaller Value

The easiest fix for this issue is to just update the columns and specify the value of the particular field to 255 characters.
<?php $table->string('email', 255);

Method 2: Update config/database.php Configuration

The next fix is to update the "configuration database" which is located in the "config" folder.
The value of the charset and collation should be updated to below.

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

Method 3: Update defaultStringLength value

The 3rd method is to update the defaultStringLength to have a value of 191 and this should fix the error.


Schema::create('users', function (Blueprint $table) {
    // your columns definition
notion avatar


Week-end developer currently experimenting with web, mobile, and all things programming.






Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this