Laravel 5.4 php artisan migrate 报错

xiaoxiao2021-02-27  207

Laravel5.4 默认使用 utf8mb4 字符,包括支持在数据库存储「表情」。如果你正在运行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,为了MySQL为它们创建索引,你可能需要手动配置迁移生成的默认字符串长度,你可以通过调用 AppServiceProvider 中的 Schema::defaultStringLength 方法来配置它。

执行Laravel自带的两个migrations报出以下两个错误,但是刷新数据库表依然创建出来了,那么出错的原因应该就是mysql的版本过低

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_uniqu e`(`email`)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

报错的问题就是max key length too long

那么现在只需要在原本的users migrations代码中添加这一行

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

现在输入php artisan migrate即可正常运行

转载请注明原文地址: https://www.6miu.com/read-9194.html

最新回复(0)