在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();
        });
    }

其他回答

如果您运行“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

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

Schema::defaultStringLength(191);

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

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

to

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

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

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

我用Laravel 5.7做的。

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

在laravel

首先将默认数据库引擎设置为“InnoDB on”

/ config / database.php

'engine' => 'InnoDB',

然后运行PHP artisan config:cache来清除和刷新配置缓存。

PHP工匠数据库:擦

修改/config/database.php中mysql数组的这些值,如下所示 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 然后

PHP工匠迁移 这是所有!迁移表创建成功。

对我来说很有魅力!

将此添加到config/database.php

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

而不是

'engine' => 'null',

对于不想更改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();