首页 > 代码库 > Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))
Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))
Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))
public function up() { Schema::create(‘users‘, function (Blueprint $table) { $table->increments(‘id‘); $table->string(‘name‘); $table->string(‘email‘)->unique(); $table->string(‘password‘); $table->rememberToken(); $table->timestamps(); }); }
以上是user表的migartion,可以看出name字段并没有声明长度,laravel默认了1071,而报错中看出数据库设置了最大是767,所以就报错了
Laravel 5.4默认使用utf8mb4
字符编码,而不是之前的utf8
编码。mb4的最大字符长度为4个字节,解决方法是:
手动配置迁移命令migrate生成的默认字符串长度,在AppServiceProvider中调用Schema::defaultStringLength方法来实现配置:
use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { // 767/4 Schema::defaultStringLength(191); }
Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。