Laravel 5.4: 特殊字段太長報錯

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

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

[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-indexesapp

******ide

相關文章
相關標籤/搜索