duplicate entry for key 'idx_client_id_parent_

2019-08-12 16:39发布

问题:

Joomla 3

I am trying to manually insert some records into #__menu. Since for most fields I can just use the value that other records have, I am trying to get a stdObject from an existing record and insert it back to the table. Before that, I need to take care of the possible key duplicate. I read the table structure, and besides id, I find field lft and rgt seem to have to be unique. So the following is what I try:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sql = 'SELECT * FROM `#__menu` WHERE id = 203';
$db->setQuery($sql);
$example = $db->loadObjectList()[0];
unset($example->id);                     
unset($example->lft);
unset($example->rgt);

$db->insertObject('#__menu',$example);

The error message I get is

Duplicate entry '0-1-001-*' for key 'idx_client_id_parent_id_alias_language'

SQL=INSERT INTO #__menu (menutype,title,alias,note,path,link,type,published,parent_id,level,component_id,checked_out,checked_out_time,browserNav,access,img,template_style_id,params,home,language,client_id) VALUES ('hidden','test','001','','001','index.php?option=com_k2&view=item&layout=item&id=1','component','1','1','1','10125','0','0000-00-00 00:00:00','0','1',' ','0','{\"menu-anchor_title\":\"\",\"menu-anchor_css\":\"\",\"menu_image\":\"\",\"menu_text\":1,\"menu_show\":1,\"page_title\":\"\",\"show_page_heading\":\"\",\"page_heading\":\"\",\"pageclass_sfx\":\"\",\"menu-meta_description\":\"\",\"menu-meta_keywords\":\"\",\"robots\":\"\",\"secure\":0}','0','*','0')

I don't understand why there is a key called 'idx_client_id_parent_id_alias_language', it sure is not one of the table's fields. Googling it returns some result, but it seems to me none of them is related to my problem.

回答1:

Thought that I would write up an answer as shenkwen commented that he googled for an answer and found nothing helpful.

The error that OP is seeing as caused by him manually inserting a row but there is a multiple column key on the menu table. The key is a combination of client_id, parent_id, alias and language.

You can not have two rows which share those same values. As noted in the comments from OP he was duplicating the alias.



标签: php mysql joomla