我有时会在我的应用程序日志中收到以下错误以插入 MySQL:
2013/06/02 15:11:05 (Database) Exception: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4d9acbacc6ed7dc2733025e4b8af7be4940fe5b70338789b34caea27695860fa' for key 'hash_UNIQUE'
上面的问题是被索引的值被截断为 64 个字符长,但实际列大小为 255 个字符长。该值的一个示例是:4d9acbacc6ed7dc2733025e4b8af7be4940fe5b70338789b34caea27695860fa2b497f6cd34851657c9d3e8e8e88713d80da1a22d8654cf98622e9af3f29481d
。
表定义如下:
delimiter $$
CREATE TABLE `reset` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`identity_id` bigint(20) unsigned NOT NULL,
`hash` varchar(255) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`upn` varchar(255) NOT NULL,
`returnurl` varchar(255) DEFAULT NULL,
`processed` char(1) DEFAULT 'N',
`referer` varchar(15) DEFAULT 'CPCUsociety.org',
PRIMARY KEY (`id`),
UNIQUE KEY `hash_UNIQUE` (`hash`),
KEY `reset_upn_ndx` (`upn`),
KEY `reset_hash_ndx` (`hash`),
KEY `reset_timestampndx` (`timestamp`),
KEY `idendity_id_for_key_idx` (`identity_id`),
CONSTRAINT `idendity_id_for_key` FOREIGN KEY (`identity_id`) REFERENCES `identity` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=585 DEFAULT CHARSET=utf8$$
如何强制 MySQL 使用完整值作为索引?