Mysql :: Error:指定的密钥太长;最大密钥长度为767个字节:CREATE INDEX [英] Mysql::Error: Specified key was too long; max key length is 767 bytes: CREATE INDEX

查看:106
本文介绍了Mysql :: Error:指定的密钥太长;最大密钥长度为767个字节:CREATE INDEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 2.3.5应用程序,在女巫中我有此字段

t.string   "trip_cities",             :limit => 256

这个索引

add_index "bookings", ["trip_cities"], :name => "trip_cities"

当我尝试执行时:

bundle exec rake db:test:load

我收到此错误Mysql::Error: Specified key was too long; max key length is 767 bytes: CREATE INDEX 'trip_cities' ON 'bookings' ('trip_cities'),并且不太了解如何解决此问题.

解决方案

这听起来像默认排序规则使用UTF8字符集.

MySQL通过字节而不是字符来限制键的长度.由于MySQL使用的UTF8实现允许每个字符使用3个字节,因此UTF8列上键的最大长度是键长度(以字符为单位)的3倍(如果未明确指定,则键长度为字段的全长).

在这种情况下,最大密钥长度将为256 * 3,即768.您需要限制键的长度或更改列的排序规则.

I'm working with rails 2.3.5 application, in witch I have this field

t.string   "trip_cities",             :limit => 256

And this index

add_index "bookings", ["trip_cities"], :name => "trip_cities"

When I try to execute:

bundle exec rake db:test:load

I receive this error Mysql::Error: Specified key was too long; max key length is 767 bytes: CREATE INDEX 'trip_cities' ON 'bookings' ('trip_cities') and don't quite know how to resolve this.

解决方案

It sounds like the default collation uses the UTF8 character set.

MySQL limits the length of keys by bytes, not characters. Since the UTF8 implementation MySQL uses allows for 3 bytes per character, the max length of a key on a UTF8 column is 3 times the key length in characters (the key length is the full length of the field if not explicitly specified).

In this case the max key length would be 256 * 3 which is 768. You need to either limit the length of the key or change the collation of the column.

这篇关于Mysql :: Error:指定的密钥太长;最大密钥长度为767个字节:CREATE INDEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆