Laravel 5.4 migrate報錯:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))


Laravel 5.4 migrate報錯:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us     ers_email_unique`(`email`))

 

 

 

 

 

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

以上是user表的migartion,可以看出name字段並沒有聲明長度,laravel默認了1071,而報錯中看出數據庫設置了最大是767,所以就報錯了

Laravel 5.4默認使用utf8mb4字符編碼,而不是之前的utf8編碼。mb4的最大字符長度為4個字節,解決方法是:

手動配置遷移命令migrate生成的默認字符串長度,在AppServiceProvider中調用Schema::defaultStringLength方法來實現配置:

use Illuminate\Support\Facades\Schema;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
   //     767/4  
   Schema::defaultStringLength(191);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM