解决 Laravel 错误 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

/ 0评 / 0

Laravl在5.4的时候改了默认的字符集为utfmb4,支持emoji。如果数据库是mysql 5.7.7以上的话,应该是兼容的。但如果用的是mariadb或者旧的mysql版本,那么就会提示以下错误 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes要解决错误,在AppServiceProvider.php添加代码

<span class="hljs-selector-tag">use</span> <span class="hljs-selector-tag">IlluminateSupportFacadesSchema</span>;

<span class="hljs-selector-tag">public</span> <span class="hljs-selector-tag">function</span> <span class="hljs-selector-tag">boot</span>()
{
    <span class="hljs-attribute">Schema</span>::<span class="hljs-built_in">defaultStringLength</span>(<span class="hljs-number">191</span>);
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注