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

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

Back to Laravel

Need help with Linux Server or WordPress? We can help!

Leave a Reply

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