Laravel migrate 报错:Syntax error or access violation: 1071 Specified key was too long; max key long

问题:

运行:php artisan migrate
报错: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

图片.png

解决办法: 在 SegmentFault上查找到的解决方法

解释:在官方文档里找到的答案

Index Lengths & MySQL / MariaDB
Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them. You may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

注意:在 AppServiceProvider里添加的是以下两行代码:

use Illuminate\Support\Facades\Schema;
Schema::defaultStringLength(191);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容