在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


当前回答

模式: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();
        });
    }

其他回答

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

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

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

如前所述,我们在App/Providers中添加到AppServiceProvider.php

use Illuminate\Support\Facades\Schema;  // add this

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

你可以在下面的链接中看到更多细节(搜索“索引长度& MySQL / MariaDB”) https://laravel.com/docs/5.5/migrations

BUT WELL THAT's not what I published all about! the thing is even when doing the above you will likely to get another error (that's when you run php artisan migrate command and because of the problem of the length, the operation will likely stuck in the middle. solution is below, and the user table is likely created without the rest or not totally correctly) we need to roll back. the default roll back will not work. because the operation of migration didn't like finish. you need to delete the new created tables in the database manually.

我们可以这样做使用修补器如下所示:

L:\todos> php artisan tinker

Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman

>>> Schema::drop('users')

=> null

我自己有一个问题与用户表。

之后你就可以开始了

PHP artisan migrate:回滚

PHP工匠迁移

正如迁移指南中所概述的那样,你所要做的就是编辑app/Providers/AppServiceProvider.php文件,并在boot方法中设置默认字符串长度:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

注意:首先你必须从数据库中删除(如果你有)users表,password_resets表,并从迁移表中删除users和password_resets条目。

要运行所有未完成的迁移,执行migrate Artisan命令:

php artisan migrate

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

据官方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选项。有关如何正确启用此选项的说明,请参阅数据库的文档。

最干净的解决方案是去你的数据库并更改:

default_storage_engine to InnoDB

你很可能有MyISAM。