Laravel Database Migration Error Key too long

When doing a database migration on Laravel, I get the following error

$ php artisan migrate

  [Illuminate\Database\QueryException]
  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`))

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
$ 

This is because MariaDB uses a different UTF8 format.

To fix, edit file

Advertisement

vi ./app/Providers/AppServiceProvider.php

Inside, find

    public function boot()
    {
        //
    }

Replace with

    public function boot()
    {
        Schema::defaultStringLength(191);
    }

You will also need to add

use Illuminate\Support\Facades\Schema;

here is what my file looks like after editing.

$ cat ./app/Providers/AppServiceProvider.php
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

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()
    {
        //
    }
}
$ 
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement