Laravel Error: 1071 Specified key was too long

laravel-framework-help-and-guide
print

We’ve all been there.  We’ve installed a fresh new Laravel installation, come to do the migrations and hit the dreaded error message saying our email key was too long.

This is the message in question:

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

Thankfully, its really easy to fix.

Find the file: [your webroot]/app/Providers/AppServiceProvider.php

It will look like this from the default installation:

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Simply change to look like this:

<?php


namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

.. and all will be well 🙂

 

Share this post:
Related Posts
Getting the value or text of an HTML attribute using jQuery
Stop jQuery ignoring the if/else conditions

Leave a Comment