Why is it limited to 191 characters?
The key is that Laravel 5.4 defaults to utf8mb4
so the database assumes a maximum of 4 bytes for each character.
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
如果对utf8mb4进行了更新,那么您所引用的示例将完美匹配.Emphasis在编辑处添加.
The index key prefix length limit is 767 bytes for InnoDB tables that use the REDUNDANT or COMPACT row format. For example, you might hit this limit with a column prefix index of more than 191 characters on a TEXT or VARCHAR column, assuming a utf8mb4 character set and the maximum of 4 bytes for each character.
191 * 4 = 764
(作品)
192 * 4 = 768
(too large)
utf8mb4
引入了对Supplementary Characters
的支持,这不是utf8
的一部分,其中包括表情符号.为了进一步了解你是否需要utf8mb4
,你可以从this StackOverflow answer on the question开始.
How do I fix this?
如果需要保持较大的字段大小,最快的修复方法是将默认值改为utf8
.Laravel 5.3 and below defaulted utf8
, and that would be safe to use in most cases.
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Alternatively, you can set the charset
and collation
on specific tables in the DB Migration when creating that table. That would look something like this:
Schema::create('monitors', function (Blueprint $table) {
$table->charset = 'utf8';
$table->collation = 'utf8_unicode_ci';
$table->increments('id');
$table->string('myString')->unique();
});
What If I need utf8mb4
and long index keys?
This StackOverflow answer应该是一个很好的起点.它讨论了设置innodb_large_prefix
以允许更长的密钥长度.