Laravel 5.4 migrate报错

本文解决Laravel5.4迁移时遇到的Syntax error or access violation:1071 Specified key was too long; max key length is 767 bytes错误。详细介绍了如何调整字符串字段长度以匹配数据库限制,通过修改AppServiceProvider中的Schema::defaultStringLength方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值