How to Fix Specified Key Was Too Long Error - Laravel 5.4 | South Coast Web Design Ltd
South Coast Web Design Ltd
How to Fix Specified Key Was Too Long Error – Laravel 5.4 Feb 22

Laravel 5.4 made a change to the default database character set, which will cause issues for anyone that is trying to run their new project using Older versions of MySQL (5.7.7 and below) or MariaDB.

It’s default charset is now utf8mb4 which includes support for storing emojis.

Recently I have tried migrating a project across from local development server to a production server running PHP 5.6 and MySQL 5.6 which are classed as STABLE RELEASE.

On trying to import the DB into phpmyadmin it crashes with the following error:

Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

However there is a migration fix which is listed within the documentation here (towards the bottom of the page)

Just open your project and find the AppServiceProvider.php inside app\providers directory.

And add the following line within the boot function:

	 	 
<!--?php 	 	 
namespace App\Providers;	 	 
use Illuminate\Support\ServiceProvider;

// add schema - required for fix to work 	 
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider	 	 
{	 	 
   /**	 	 
   * Bootstrap any application services.	 	 
   *	 	 
   * @return void	 	 
   */	  
   public function boot()	 	 
   {	 	 
         // use schema to set default length - fix 	 
         Schema::defaultStringLength(191);	 	 
   }	 	
 
   /**	 	 
   * Register any application services.	 	 
   *	 	 
   * @return void	 	 
   */	 	 
   public function register()	 	 
   {	 	 
         //	 	 
   }	 	 
}	
 	 

This should now fix the issue. You may need to restart your database for the changes to take effect.<br ?–>
However as practice we now add this line by default within our projects as most production servers are running the STABLE releases by default.

Leave a Response


Top