SQLSTATE 42000: Syntax error or access violation – Laravel Solved

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

Have you ever get SQLSTATE 42000 error in PHP, Yes of course this error occurs several time when you’re using PDO connection in PHP. And in this article i’m going to cover why this error occurs in Laravel and how to solve in this error in laravel framework.

Why this error occurs in Laravel?

In Laravel 5.4 and above the default database character set is changed to utf8mb4 which supports for the storing of Emojis. This error occurs on MySQL version less than 5.7.7 and MariaDB version less than 10.1 and higher you do not need to do anything.

[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

How to solve SQLSTATE 42000 error in Laravel

To solve this error, locate app/Providers/AppServiceProvider.php and add Schema::defaultStringLength(191); in boot method as following.

Once you have added now you’re good go. Now just run php artisan migrate your error will be solved.

If problem persist again just leave a comment in comment box. I’m happy to guide you.

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.