Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Demers94's avatar

Syntax error or access violation: 1071 Specified key was too long

Hi,

I've just upgraded to Laravel 5.4 and created my first project with it. When I first try to migrate the database, I get this error :

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

I can fix this by going in the migration for the users table, and manually specifying the max-length of the email field, like so :

$table->string('email', 250)->unique();

If I run php artisan migrate again, it will work for the users table, but will fail for the password reset table. I have to do the same thing for the email field in that migration.

Has anyone else encountered this issue? It was working fine before with 5.3.

I'm running Windows 10 and using HeidiSQL to create my database, like I've always used before.

0 likes
60 replies
jtubbs's avatar

I have the same error on OSX with Valet and MariaDB.

1 like
Demers94's avatar

@jtubbs I've opened an issue in the Github page of the framework, you can follow the progress here : https://github.com/laravel/framework/issues/17508

The solution was to add this line in the boot() method of the AppServiceProvider :

Schema::defaultStringLength(191);

(don't forget to import the namespace)

You can read about this in the migrations' documentation : https://github.com/laravel/docs/blob/5.4/migrations.md#index-lengths--mysql

27 likes
Hagosm's avatar

@Demers94 use Illuminate\Support\Facades\Schema; // At the top of your file Schema::defaultStringLength(191);

imran's avatar

Schema::defaultStringLength(191); works well

3 likes
carrie007's avatar

@imran Worked for me too. Let me share, I've been trying to do this for 3 days with what looked like an older version to Laravel. I updated Laravel to the latest and all of my problems but this one went away. Well, now that problem is gone too. Thanks so much guys!

carrie007's avatar

I do have this issue now:

ErrorException Undefined variable: activePage (View: C:\wamp64\www\MyWebApp\resources\views\users\index.blade.php)

tahertechs's avatar

Class App\Providers\Schema not found

After adding that line I get that error

Demers94's avatar

@tahertechs You need to import the Schema namespace at the top.

use  Illuminate\Support\Facades\Schema; // At the top of your file
13 likes
willrobinson's avatar

This workaround works but doesn't this mean that you can't have E-Mail addresses of more than 191 characters long? IIRC, the standard allows for E-Mail addresses that are up to 254 characters in length.

ejdelmonico's avatar

If you are using MariaDB or an older version of MySQL, you need to place this code in your AppServiceProvider.php:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
19 likes
msyadav's avatar

I placed the same, but while installing spatie/ roles and permissions package and doing migrate , it gives error : Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table permissions add unique permissions_name_guard_name_unique(name, guard_name))

2 likes
Abby2727's avatar

@msyadav In your database.php replace the following with this: 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'engine' => env('DB_ENGINE', 'InnoDB'),

then php artisan optimize, try to migrate again :>

k32y's avatar

This fixed it for me:

Inside config/database.php, replace this line for mysql

'engine' => null',

with

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

Instead of setting a limit on your string lenght.

42 likes
elocke2017's avatar

@k32y I tried your solution, but then I get a new error in another of my tables, which says you cannot add a foreign key constraint. SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Is there another engine option for MySQL which can solve the UTF8MB4 issue as well as allow FK constraints in the standard way, or can MyISAM handle it with some other small change? I'm running MySQL 5.7.11 locally, but the application will eventually run in the cloud and needs to be robust for any MySQL db.

Thank you for any help.

https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html "InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order. InnoDB does not currently support foreign keys for tables with user-defined partitioning. This means that no user-partitioned InnoDB table may contain foreign key references or columns referenced by foreign keys."

migas's avatar

Great! It work's fine. Thanks for the help!

k32y's avatar

@elocke2017 Your problem is not with InnoDB. MyISAM does not support foreign key and therefore won't work. The problem is more likely one of the following:

  • Your tables have different storage engines, try setting the storage engine explicitly with $table->engine('InnoDB');.
  • Your foreign key is being referenced before it's created. You can change the order your migrations are run by changing the filename timestamp.
  • There is datatype mismatch between the keys.
  • etc.
2 likes
Snapey's avatar

I found easiest to set in migration;

        Schema::defaultStringLength(191);
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

5 likes
marcgaumont's avatar

Can someone please explain why this is happening ?

I can't even use factory's or seeding properly anymore... I try to seed my CRM with companies and if I set my seeder to say "5", all is fine... if I try 50... I get that error... AND I am using

Schema::defaultStringLength(191);

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;

Any real fix?

Cronix's avatar

@marcgaumont You can try changing the database config back to using utf8 instead of utf8mb4, which was the change L5.4 made. You'd have to change the database schema and any tables you've made to use utf8 as well.

1 like
Cronix's avatar

Also make sure your table names aren't too long. For relationship tables, it can create some very large index names using a combination of the 2 table names and also adding on some other stuff like "_index". I don't recall exactly off the top of my head what it adds. You might have to explicitly name the indexes when creating indexes and FKs for longer table name combinations instead of letting laravel do it automatically. Hard to explain but I've had this issue a few times.

nick.a's avatar

Just in case anyone is wondering, as Cronix mentioned Laravel 5.4 is in fact using utf8mb4. If you're running MySQL prior to 5.7.7 or MariaDB prior to 10.2.2 you will get this error. If you want to use utf8mb4 then just upgrade your MySQL, MariaDB, etc. to the most recent or development versions.

Demers94's avatar

@Nicholas I saw that mentionned in the Github thread, but I'm running into this issue even with 5.7.14 and utf8mb4. I never managed to get it to work on any of my computers.

Adding the Schema line in the AppServiceProvider or changing the engine option in config/database.php works, but it's a bit annoying to have to remember to do that every new project.

marcorieser's avatar

Updating and restarting MariaDB (homebrew) worked for me without any of the fixes.

Snapey's avatar

@stephy do you want to retype your rant please, we don't tolerate shouting and swearing here...

There are plenty of options listed in this post to get around the fact that you are using an old version of mysql

Next

Please or to participate in this conversation.