Wednesday, 8 August 2018

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



The error message is pretty straightforward: it indicates that an operation on a database table was halted because the size of the index being created exceeds the maximum allowed limit of 767 bytes. This limit is inherent to the InnoDB storage engine, which is the default for MySQL versions 5.6 and above. The error is most commonly encountered when working with VARCHAR or TEXT fields that are set to be unique or indexed, and the character set is utf8mb4, which allows for storing emojis and other multibyte characters.

Possible solutions :

Solution :

1) Go to the path in your Laravel installation


app/Providers/AppServiceProvider.php


or to see the default Laravel installation file AppServiceProvider.php
go here

https://github.com/laravel/laravel/blob/master/app/Providers/AppServiceProvider.php

In your file AppServiceProvider.php
 will see the method boot()






2) Simply update boot() method as like below:


use Illuminate\Support\Facades\Schema;


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

}  


5) Use a Different Character Set: 

If using utf8mb4 is not mandatory, switching to a character set that requires fewer bytes per character, like utf8, can also resolve the issue. However, this may not be suitable for all applications, especially those that need to store a wide variety of international characters.


3) Change the Database Engine:

    If altering the column length is not feasible or desirable, another approach is to switch to a database engine that supports larger index sizes. For MySQL, upgrading to version 5.7.7 or higher allows for the use of the innodb_large_prefix option, which increases the maximum index length to 3072 bytes.


  1. 4) Modify the Server Configuration:

  2. For those who cannot upgrade MySQL or change the engine, modifying the MySQL or MariaDB server configuration can provide a workaround. Setting the innodb_file_format to Barracuda and innodb_large_prefix to 1 in the server's my.cnf file can help overcome this limitation.

No comments:

Post a Comment

Laravel csrf token mismatch for ajax post request

Error "CSRF Token Mismatch" commonly occurs if tokens do not match in both sessions and sent , and received requests.CSRF token he...