SQLSTATE[42000]:语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节

Ban*_*eee 6 mysql laravel

在 cmd 中提交“php artisan migrate:fresh”后出现错误。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
Run Code Online (Sandbox Code Playgroud)

SQLSTATE[42000]:语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节(SQL:alter table usersadd unique users_email_unique( email))

Doh*_*h09 10

从这个链接:https : //laravel-news.com/laravel-5-4-key-too-long-error

对于那些运行 MariaDB 或旧版本 MySQL 的用户,您可能会在尝试运行迁移时遇到此错误

如迁移指南中所述,您只需编辑 AppServiceProvider.php 文件并在 boot 方法中设置默认字符串长度即可解决此问题:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
Run Code Online (Sandbox Code Playgroud)

之后一切都应该正常工作。

请注意,根据下面 Andrew Koster 的评论,这可能只是用于遗留代码的解决方案。您可能希望为最新项目研究不同的解决方案。