I'm trying to upgrade the character encoding from latin1
to utfmb8
in a MySQL database (part of a Rails 3 app). I've done some reading but I'm stuck on my last two tables: users
and trigrams
.
Trying to update the users
table gets the dreaded ERROR 1071
:
mysql> ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
Following this answer, I can work around that by dropping and recreating the index on the problematic VARCHAR(255)
column:
mysql> DROP INDEX index_users_on_remember_token ON users;
mysql> CREATE INDEX index_users_on_remember_token ON users (remember_token(191));
I'm worried that'll screw things up down the line, since I don't really understand indexes. But putting that aside for the moment...
My main problem is how to do something similar for my last table: trigrams
. Here's some info on the trigrams
table:
mysql> SHOW CREATE TABLE trigrams;
CREATE TABLE `trigrams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trigram` varchar(3) DEFAULT NULL,
`score` smallint(6) DEFAULT NULL,
`owner_id` int(11) DEFAULT NULL,
`owner_type` varchar(255) DEFAULT NULL,
`fuzzy_field` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_for_match` (`owner_id`,`owner_type`,`fuzzy_field`,`trigram`,`score`),
KEY `index_by_owner` (`owner_id`,`owner_type`)
) ENGINE=InnoDB AUTO_INCREMENT=41531 DEFAULT CHARSET=latin1
mysql> SHOW INDEXES IN trigrams;
+----------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| trigrams | 0 | PRIMARY | 1 | id | A | 35502 | NULL | NULL | | BTREE | | |
| trigrams | 1 | index_for_match | 1 | owner_id | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_for_match | 2 | owner_type | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_for_match | 3 | fuzzy_field | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_for_match | 4 | trigram | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_for_match | 5 | score | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_by_owner | 1 | owner_id | A | 35502 | NULL | NULL | YES | BTREE | | |
| trigrams | 1 | index_by_owner | 2 | owner_type | A | 35502 | NULL | NULL | YES | BTREE | | |
+----------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
My question is: how do I get around the following error?
mysql> ALTER TABLE trigrams CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
What changes do I make to trigrams
? And what are the risks of making those changes?
(If it's relevant: the trigrams
table was automatically created by some third-party code, the fuzzily gem.)