How to fix the MySQL related "Specified key was too long error" error in Laravel

Laravel: How to fix the MySQL related "Specified key was too long error" error in Laravel

This is a common problem (especially if you use a local development environment such as XAMPP).

The error will look something like this:

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

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

Luckily the fix is simple. Add the following to the boot() method of your app/Providers/AppServiceProvider.php file:

  1. Schema::defaultStringLength(191);

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to fix the MySQL related "Specified key was too long error" error in Laravel

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

How to group a SQL statement in brackets/parenthesis

How to set the Expires header in .htaccess

How to disable file hotlinking in Apache with .htaccess

How to add the default charset to UTF-8 in .htaccess

How to find out which php.ini file PHP is using?

How to convert between types in JS?

What is the loop variable in a @foreach loop in Blade?

How to fix the MySQL related "Specified key was too long error" error in Laravel

How to block access to certain referrers in Apache using .htaccess