Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py	(revision 2961)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py	(working copy)
@@ -581,6 +581,10 @@
             save_sql = "INSERT INTO device(id, zone, name, ip_address, protocol, restapi_port, restapi_username, restapi_password, console_username, console_password, connection, status, license_key, gateway_domain, location, firewall_ip, intranet_ip, version, type, own, log_enable, webui_port, device_group, enable_password) Values " + "('%(id)s', '%(zone)s', '%(name)s', '%(ip)s', '%(protocol)s', '%(restapi_port)s', '%(restapi_username)s', '%(restapi_password)s', '%(console_username)s', '%(console_password)s', 'connected', '%(status)s', '%(license_key)s', '%(gateway_domain)s', '%(location)s', '%(firewall_ip)s', '%(intranet_ip)s', '%(version)s', '%(type)s', '%(own)s', '%(log_enable)d', '%(webui_port)d', '%(device_group)s', '%(enable_password)s')"%data
             db.execute_sql(save_sql)
             create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+            # Guard against stale orphan rows left by a previously failed deletion.
+            # Remove any existing device-type entry for this name before inserting.
+            cleanup_sql = "DELETE FROM file_list WHERE name='%s' AND type='device'" % data['name']
+            db.execute_sql(cleanup_sql)
             insert_config_sql = "INSERT INTO file_list(name, create_time, modify_time, type, comment, device_type, device_id) values('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (data['name'], create_time, create_time, 'device', '', data['type'], data['id'])
             db.execute_sql(insert_config_sql)
             db.close()
@@ -945,9 +949,15 @@
 
             L.remove_lock(data['ip'].values()[0])
 
-            #delete from file_list, delete device item and backup item
+            #delete from file_list, delete backup items
+            # Backup entries store the device name in the modify_time column (legacy schema).
             delete_sql = "DELETE FROM file_list WHERE modify_time= '%s'" % data['name']
             db.execute_sql(delete_sql)
+            # Delete the device config entry by name+type. The device-type entry stores
+            # an actual datetime in modify_time (not the device name), so the DELETE above
+            # would silently skip it - causing a duplicate key error when re-adding.
+            delete_sql = "DELETE FROM file_list WHERE name='%s' AND type='device'" % data['name']
+            db.execute_sql(delete_sql)
 
             #remove the backup config file
             for item in backup_result:
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py	(revision 2961)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py	(working copy)
@@ -245,6 +245,11 @@
 
     def updata_time(self, model_instance, fields=None):
         key = '.'.join(model_instance._meta._model._meta.path) + model_instance.pk_str()
+        if key not in self._cache_time:
+            if key not in self._cache_dict:
+                andebug('hive.debug', 'Skipping updata_time for deleted instance: %s' % key)
+                return
+            self._cache_time[key] = {'_instance': time.time()}
         try:
             if fields:
                 for f in fields:
@@ -252,7 +257,7 @@
             else:
                 self._cache_time[key] = {'_instance':time.time()}
         except KeyError:
-            anerror('hive.debug', 'Fail updating time with invalid key: %s' % key)
+            andebug('hive.debug', 'Fail updating time with invalid key: %s' % key)
 
     def get_time(self, model_instance, field):
         key = '.'.join(model_instance._meta._model._meta.path) + model_instance.pk_str()
@@ -262,7 +267,7 @@
             else:
                 return self._cache_time[key]['_instance']
         except KeyError:
-            anerror('hive.debug', 'Fail getting time with invalid key: %s' % key)
+            andebug('hive.debug', 'Fail getting time with invalid key: %s' % key)
             return None
 
     def remove_time(self, model_instance, field):
@@ -271,7 +276,7 @@
             if field in self._cache_time[key]:
                 del self._cache_time[key][field]
         except KeyError:
-            anerror('hive.debug', 'Fail removing time with invalid key: %s' % key)
+            andebug('hive.debug', 'Fail removing time with invalid key: %s' % key)
         
     def merge(self, qs):
         ret = []
