在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


当前回答

如果您运行“php artisan migrate”时出现此错误。你可以这样修改你想要更新的表:

    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        

在迁移脚本中。例子:

class MyMigration extends Migration {

/**
 * Run the migrations.
 */
public function up()
{
    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        
    Schema::table('table_name', function ($table) {
        //....
    });
}

/**
 * Undo the migrations.
 */
public function down()
{
    //....
}
}

然后再次运行php artisan migrate

其他回答

对于不想更改AppServiceProvider.php的人。 (在我看来,仅仅为了迁移而更改AppServiceProvider.php是一个坏主意)

您可以将数据长度添加回database/migrations/下的迁移文件,如下所示:

create_users_table.php

$table->string('name',64);
$table->string('email',128)->unique();

create_password_resets_table.php

$table->string('email',128)->index();

对于这个错误,我找到了两个解决方案

选项1:

打开database/migrations文件夹下的user和password_reset表

只需要改变邮件的长度:

$table->string('email',191)->unique();

选项2:

打开app/Providers/AppServiceProvider.php文件,在boot()方法中设置一个默认字符串长度:

use Illuminate\Support\Facades\Schema;

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

如果您运行“php artisan migrate”时出现此错误。你可以这样修改你想要更新的表:

    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        

在迁移脚本中。例子:

class MyMigration extends Migration {

/**
 * Run the migrations.
 */
public function up()
{
    DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');        
    Schema::table('table_name', function ($table) {
        //....
    });
}

/**
 * Undo the migrations.
 */
public function down()
{
    //....
}
}

然后再次运行php artisan migrate

我添加了两个对我有用的解决方案。

第一个解决方案是:

在config目录下打开database.php文件。 将'engine' =>修改为'engine' => 'InnoDB' 这对我很管用。

第二个解决方案是:

在config目录下打开database.php文件。 2.编辑'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 来 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci',

古德勒克

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

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

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