开发者

MySQL: Duplicate Entry for Key 2

I'm not sure what I'm doing to cause this error. The query:

INSERT INTO node (type, language, title) VALUES ('bout', 'en', 'the title 3')

The error:

#1062 - Duplicate entry '0' for key 2 

The table:

CREATE TABLE `node` (
  `nid` int(10) unsigned NOT NULL auto_increment,
  `vid` int(10) unsigned NOT NULL default '0',
  `type` varchar(32) NOT NULL default '',
  `language` varchar(12) NOT NULL default '',
  `title` varchar(255) NOT NULL default '',
  `uid` int(11) NOT NULL default '0',
  `status` int(11) NOT NULL default '1',
  `created` int(11) NOT NULL default '0',
  `changed` int(11) NOT NULL default '0',
  `comment` int(11) NOT NULL default '0',
  `promote` int(11) NOT NULL default '0',
  `moderate` int(11) NOT NULL default '0',
  `sticky` int(11) NOT NULL default '0',
  `tn开发者_开发知识库id` int(10) unsigned NOT NULL default '0',
  `translate` int(11) NOT NULL default '0',
  PRIMARY KEY  (`nid`),
  UNIQUE KEY `vid` (`vid`),
  KEY `node_changed` (`changed`),
  KEY `node_created` (`created`),
  KEY `node_moderate` (`moderate`),
  KEY `node_promote_status` (`promote`,`status`),
  KEY `node_status_type` (`status`,`type`,`nid`),
  KEY `node_title_type` (`title`,`type`(4)),
  KEY `node_type` (`type`(4)),
  KEY `uid` (`uid`),
  KEY `tnid` (`tnid`),
  KEY `translate` (`translate`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2586 ;

What am I doing wrong? By not specifying a nid for the new entry, it will auto-increment, right?


You have vid as a unique key. However you never set a value for it so the default 0 is always used. Second entry into the table will violate uniqueness.


The problem looks to be with vid not being specified. The first entry you put will have had a vid of 0 (the default) and the next will try 0 again and fail on the UNIQUE index. Not that only the primary key, nid, will auto-increment.


I think nid isn't the problem, vid is. It defaults to 0 and is designated unique.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜