In Laravel 5.4, migrate reports the following error: Specified key was too long error solution: laravelmigrate
Preface
As we all know, we often develop projects by team collaboration. Everyone is in their own local database. If you have asked your colleagues to manually add fields to the database structure, database migration can solve this problem.
In addition, online deployment also avoids the trouble of manually importing a database or manually modifying the data structure. data migration helps you easily maintain the data structure.
However, this article will introduce the solution to the migrate error Specified key was too long error in Laravel5.4, let's take a look at the detailed introduction.
Problems Found
Laravel 5.4 uses utf8mb4 character encoding by default, instead of the previous utf8 encoding. Thereforephp artisan migrate
The following error occurs:
[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
Root Cause
MySql supports UTF-8 encoding with a maximum length of 3 bytes. If a 4-byte width character is entered, an insertion exception occurs. The maximum encoded Unicode Character of the Three-byte UTF-8 is 0 xffff, that is, the basic multilingual plane (BMP) in Unicode ). Therefore, Unicode characters, including Emoji expressions (Emoji is a special Unicode code), in non-basic multi-text flat, cannot be stored using the utf8 Character Set of MySql.
This should be one of the reasons for Laravel 5.4 to use a 4-byte utf8mb4 character encoding. Note that utf8mb4 character encoding is supported only after MySql 5.5.3 (view version: selection version ();). If the MySql version is too low, you need to update the version.
Note:If you upgraded from Laravel 5.3 to Laravel 5.4, you do not need to switch the character encoding.
Solve the problem
- Upgrade MySql to version 5.5.3 or later.
- Manually configure the default string length generated by the Migration command migrate, which is called in AppServiceProvider
Schema::defaultStringLength
Method To achieve Configuration:
use Illuminate\Support\Facades\Schema; /*** Bootstrap any application services.** @return void*/public function boot(){ Schema::defaultStringLength(191);}
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.