0712-2888027 189-8648-0214
微信公眾號

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號

當(dāng)前位置:主頁 > 技術(shù)支持 > PHP > Laravel 5.4運行migrate命令報錯1071 Specified key was too long解決方案

Laravel 5.4運行migrate命令報錯1071 Specified key was too long解決方案

時間:2024-09-20來源:風(fēng)信官網(wǎng) 點擊: 1726次
Laravel 5.4運行migrate命令報錯1071 Specified key was too long解決方案如下:

D:\Laravel5.4>php artisan migrate
Migration table created successfully.


  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 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 1000 bytes

Laravel 默認(rèn)使用 utf8mb4 字符,包括支持在數(shù)據(jù)庫存儲「表情」。如果你正在運行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,為了MySQL為它們創(chuàng)建索引,你可能需要手動配置遷移生成的默認(rèn)字符串長度,你可以通過調(diào)用 AppServiceProvider 中的 Schema::defaultStringLength 方法來配置它:

use Illuminate\Support\Facades\Schema;

/**
 * 引導(dǎo)任何應(yīng)用程序服務(wù)。
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

或者你可以為數(shù)據(jù)庫開啟 innodb_large_prefix 選項,有關(guān)如何正確開啟此選項的說明請查閱數(shù)據(jù)庫文檔。

修改文件:\app\Providers\AppServiceProvider.php

修改之后的源代碼:

 <?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()
    {
        //
    }
}

重新運行php artisan migrate命令創(chuàng)建數(shù)據(jù)庫

D:\phpStudy\Laravel5.4>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
熱門關(guān)鍵詞: Laravel migrate 命令報錯 Specified key
欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開