在Laravel 5.4上使用php artisan make:auth迁移错误

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


当前回答

我刚刚修改了users和password_reset迁移文件中的以下行。

旧:$table->string('email')->unique();

新:$table->string('email', 128)->unique();

其他回答

模式:defaultStringLength (191);将默认定义所有字符串191的长度,这可能会破坏您的数据库。你不能走这条路。

只需在数据库迁移类中定义任何特定列的长度。例如,我在CreateUsersTable类中定义了“name”,“username”和“email”,如下所示:

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

据官方Laravel 7日报道。X文档,可以很容易地解决这个问题。

更新你的/app/Providers/AppServiceProvider.php包含:

use Illuminate\Support\Facades\Schema;

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

或者,您可以为您的数据库启用innodb_large_prefix选项。有关如何正确启用此选项的说明,请参阅数据库的文档。

我不知道为什么上面的解和官方的解是相加的

Schema::defaultStringLength(191);

在AppServiceProvider中不适合我。 工作的是编辑config文件夹中的database.php文件。 只是编辑

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

to

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

它应该可以工作,尽管你将无法存储扩展的多字节字符,如表情符号。

这是一个丑陋的黑客,如果你想存储字符串在非英语语言,表情符号

我用Laravel 5.7做的。

不要忘记停止并再次启动服务器。

我认为强制stringlength到191是一个非常糟糕的主意。 所以我去调查了解到底发生了什么。

我注意到这个消息错误:

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

在我更新MySQL版本后开始出现。所以我已经用PHPMyAdmin检查了表,我注意到所有创建的新表都带有utf8mb4_unicode_ci,而不是旧表的utf8_unicode_ci。

在我的doctrine配置文件中,我注意到charset被设置为utf8mb4,但我之前的所有表都是在utf8中创建的,所以我猜这是一些更新魔术,它开始在utf8mb4上工作。

现在最简单的解决方法是更改ORM配置文件中的行字符集。 然后使用utf8mb4_unicode_ci删除表(如果您在dev模式下),或者如果您不能删除它们则修复字符集。

Symfony 4

在config/packages/doctrine.yaml中将字符集:utf8mb4修改为字符集:utf8

现在我的理论迁移又开始起作用了。

需要使用以下命令创建数据库。

CREATE DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

然后运行以下命令迁移数据库表。

php artisan migrate