? 783442-mlid.patch
Index: menu_node.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/menu_node/menu_node.install,v
retrieving revision 1.2
diff -u -p -r1.2 menu_node.install
--- menu_node.install	21 Aug 2009 14:20:55 -0000	1.2
+++ menu_node.install	2 Sep 2010 17:20:38 -0000
@@ -15,7 +15,10 @@ function menu_node_schema() {
     'fields' => array(
       'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
       'mlid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0)),
-    'primary key' => array('nid', 'mlid'),
+    'primary key' => array('mlid'),
+    'indexes' => array(
+      'nid' => array('nid'),
+    ),
   );
   return $schema;
 }
@@ -75,3 +78,27 @@ function menu_node_update_6000() {
   }
   return $ret;
 }
+
+/**
+ * Erase bad records from the {menu_node} table.
+ */
+function menu_node_update_6001() {
+  $return = array();
+  $result = db_query("SELECT mn.mlid, mn.nid FROM {menu_node} mn LEFT JOIN {node} n ON mn.nid = n.nid LEFT JOIN {menu_links} ml ON ml.mlid = mn.mlid WHERE n.nid IS NULL OR ml.mlid IS NULL");
+  while ($data = db_fetch_object($result)) {
+    $return[] = update_sql("DELETE FROM {menu_node} WHERE mlid = $data->mlid AND nid = $data->nid");
+  }
+  return $return;
+}
+
+/**
+ * Update to the new schema with only mlid as primary key.
+ */
+function menu_node_update_6002() {
+  $ret = array();
+  db_drop_primary_key($ret, 'menu_node');
+  db_drop_index($ret, 'menu_node', 'nid');
+  db_add_primary_key($ret, 'menu_node', array('mlid'));
+  db_add_index($ret, 'menu_node', 'nid', array('nid'));
+  return $ret;
+}
Index: menu_node.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/menu_node/menu_node.module,v
retrieving revision 1.4
diff -u -p -r1.4 menu_node.module
--- menu_node.module	14 Sep 2009 14:26:16 -0000	1.4
+++ menu_node.module	2 Sep 2010 17:20:38 -0000
@@ -273,12 +273,14 @@ function menu_node_edit_form_submit($for
  *   The menu link id.
  */
 function menu_node_save($nid, $mlid, $hook = 'update') {
-  $new = menu_node_exists($nid, $mlid);
-  // We only save if the record does not exist, otherwise, call the update hook.
+  $new = menu_node_exists($mlid);
+  $record = array('nid' => $nid, 'mlid' => $mlid);
+  // Save if the record does not exist, otherwise update the existing link
   if (empty($new)) {
-    $record = array('nid' => $nid, 'mlid' => $mlid);
     drupal_write_record('menu_node', $record);
     $hook = 'insert';
+  } else {
+    drupal_write_record('menu_node', $record, 'mlid');
   }
   _menu_node_invoke($nid, $mlid, $hook);
 }
@@ -301,17 +303,15 @@ function _menu_node_invoke($nid, $mlid, 
 }
 
 /**
- * Check to see if a specific nid/mlid combination exists.
+ * Check to see if a specific mlid exists.
  *
- * @param $nid
- *   The node id.
  * @param $mlid
  *   The menu link id.
  * @return
  *   The count of matches (which should be 1 or 0).
  */
-function menu_node_exists($nid, $mlid) {
-  return db_result(db_query("SELECT COUNT(nid) FROM {menu_node} WHERE nid = %d AND mlid = %d", $nid, $mlid));
+function menu_node_exists($mlid) {
+  return db_result(db_query("SELECT COUNT(mlid) FROM {menu_node} WHERE mlid = %d", $mlid));
 }
 
 /**
