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


Understanding the Error

When working with Laravel and MySQL databases, developers may encounter the error related to “Specified key was too long; max key length is 767 bytes.” This issue typically appears during database migrations when Laravel tries to create indexes on string columns.

The error occurs because the InnoDB storage engine in MySQL has a maximum index length limitation. In older MySQL versions, the maximum allowed index size is 767 bytes, which can easily be exceeded when using long string columns.

Laravel applications commonly use the utf8mb4 character set, which allows storage of emojis and complex multilingual characters. However, each character in utf8mb4 can take up to 4 bytes, which increases the total index size quickly.

As a result, when you create a VARCHAR(255) column and apply an index or unique constraint, the database may exceed the allowed index size limit and trigger this error during migrations.

Laravel DomPDF Package – Generate PDF in Laravel

Understanding the Package One of the most popular tools that developers use to make PDF files directly from HTML views is the Laravel DomPDF...