Laravel 5.4: 特殊字段太長報錯


laravel 5.4 改變了默認的數據庫字符集,現在utf8mb4包括存儲emojis支持。如果你運行MySQL v5.7.7或者更高版本,則不需要做任何事情。

當你試着在一些MariaDB或者一些老版本的的MySQL上運行 migrations 命令時,你可能會碰到下面這個錯誤:

[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

我們可以在 AppServiceProvider.php 文件里的 boot 方法里設置一個默認值:

<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */
    public function boot() { Schema::defaultStringLength(191); } /** * Register any application services. * * @return void */
    public function register() { //  } }

 

原諒地址:https://laravel.com/docs/master/migrations#creating-indexes

******


免責聲明!

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



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