diff --git a/gramps/gen/db/upgrade.py b/gramps/gen/db/upgrade.py
index eaf87369d..0608b15e9 100644
--- a/gramps/gen/db/upgrade.py
+++ b/gramps/gen/db/upgrade.py
@@ -31,7 +31,7 @@ import logging
 LOG = logging.getLogger(".citation")
 
 from ..ggettext import gettext as _
-from ..constfunc import cuni
+from ..constfunc import cuni, UNITYPE
 
 """
 methods to upgrade a database from version 13 to current version
@@ -144,7 +144,9 @@ def gramps_upgrade_16(self):
                           new_citation_list, note_list, change, tag_list, 
                           private, person_ref_list)
             with BSDDBTxn(self.env, self.person_map) as txn:
-                txn.put(str(handle), new_person)
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
+                txn.put(handle, new_person)
         self.update()
 
     LOG.debug("%d persons upgraded with %d citations in %d seconds. " % 
@@ -176,7 +178,9 @@ def gramps_upgrade_16(self):
                      change, date, tag_list, private)
         LOG.debug("      upgrade new_media %s" % [new_media])
         with BSDDBTxn(self.env, self.media_map) as txn:
-            txn.put(str(handle), new_media)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_media)
         LOG.debug("      update ref map media %s" % [handle,
                         self.get_object_from_handle(handle) ])
         self.update()
@@ -212,7 +216,9 @@ def gramps_upgrade_16(self):
                          media_list, new_citation_list, note_list, 
                          change, private)
             with BSDDBTxn(self.env, self.place_map) as txn:
-                txn.put(str(handle), new_place)
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
+                txn.put(handle, new_place)
         self.update()
 
     LOG.debug("%d places upgraded with %d citations in %d seconds. " % 
@@ -260,7 +266,9 @@ def gramps_upgrade_16(self):
                           attribute_list, lds_seal_list, new_citation_list, 
                           note_list, change, tag_list, private)
             with BSDDBTxn(self.env, self.family_map) as txn:
-                txn.put(str(handle), new_family)
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
+                txn.put(handle, new_family)
         self.update()
 
     LOG.debug("%d familys upgraded with %d citations in %d seconds. " % 
@@ -300,7 +308,9 @@ def gramps_upgrade_16(self):
                          attribute_list, 
                          change, private)
             with BSDDBTxn(self.env, self.event_map) as txn:
-                txn.put(str(handle), new_event)
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
+                txn.put(handle, new_event)
         t2 = time.time()
         upgrade_time += t2 - t1
         t3 = time.time()
@@ -332,7 +342,9 @@ def gramps_upgrade_16(self):
             new_repository = (handle, gramps_id, the_type, name, note_list,
                               address_list, urls, change, private)
             with BSDDBTxn(self.env, self.repository_map) as txn:
-                txn.put(str(handle), new_repository)
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
+                txn.put(handle, new_repository)
         self.update()
 
     LOG.debug("%d repositorys upgraded with %d citations in %d seconds. " % 
@@ -535,7 +547,9 @@ def convert_source_list_to_citation_list_16(self, source_list):
                         date, page, confidence, ref, note_list, new_media_list,
                         new_data_map, new_change, private)
         with BSDDBTxn(self.env, self.citation_map) as txn:
-            txn.put(str(new_handle), new_citation)
+            if isinstance(new_handle, UNITYPE):
+                new_handle = new_handle.encode('utf-8')
+            txn.put(new_handle, new_citation)
         self.cmap_index += 1
 #        # add backlinks for references from Citation to Source
 #        with BSDDBTxn(self.env) as txn:
@@ -621,7 +635,9 @@ def gramps_upgrade_15(self):
                       )
 
         with BSDDBTxn(self.env, self.person_map) as txn:
-            txn.put(str(handle), new_person)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_person)
         self.update()
     #surname is now different, remove secondary index with names
     _db = db.DB(self.env)
@@ -644,7 +660,9 @@ def gramps_upgrade_15(self):
             new_family[13] = []
         new_family = tuple(new_family)
         with BSDDBTxn(self.env, self.family_map) as txn:
-            txn.put(str(handle), new_family)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_family)
         self.update()
 
     # ---------------------------------
@@ -661,7 +679,9 @@ def gramps_upgrade_15(self):
             new_note[6] = []
         new_note = tuple(new_note)
         with BSDDBTxn(self.env, self.note_map) as txn:
-            txn.put(str(handle), new_note)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_note)
         self.update()
 
     # ---------------------------------
@@ -674,7 +694,9 @@ def gramps_upgrade_15(self):
         new_media[10] = []
         new_media = tuple(new_media)
         with BSDDBTxn(self.env, self.media_map) as txn:
-            txn.put(str(handle), new_media)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_media)
         self.update()
 
     # ---------------------------------
@@ -688,7 +710,9 @@ def gramps_upgrade_15(self):
         #new_event[11] = []
         new_event = tuple(new_event)
         with BSDDBTxn(self.env, self.event_map) as txn:
-            txn.put(str(handle), new_event)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_event)
         self.update()
 
     # ---------------------------------
@@ -704,7 +728,9 @@ def gramps_upgrade_15(self):
         new_place = new_place[:12] + new_place[13:]
         new_place = tuple(new_place)
         with BSDDBTxn(self.env, self.place_map) as txn:
-            txn.put(str(handle), new_place)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_place)
         self.update()
 
     # ---------------------------------
@@ -717,7 +743,9 @@ def gramps_upgrade_15(self):
         new_source = new_source[:11] + new_source[12:]
         new_source = tuple(new_source)
         with BSDDBTxn(self.env, self.source_map) as txn:
-            txn.put(str(handle), new_source)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_source)
         self.update()
 
     # ---------------------------------
@@ -731,7 +759,9 @@ def gramps_upgrade_15(self):
         new_repository[5] = list(map(convert_address, new_repository[5]))
         new_repository = tuple(new_repository)
         with BSDDBTxn(self.env, self.repository_map) as txn:
-            txn.put(str(handle), new_repository)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_repository)
         self.update()
 
     # Bump up database version. Separate transaction to save metadata.
@@ -753,6 +783,8 @@ def convert_marker(self, marker_field):
             tag.set_name(tag_name)
             tag.set_priority(len(self.tags))
             with BSDDBTxn(self.env, self.tag_map) as txn:
+                if isinstance(handle, UNITYPE):
+                    handle = handle.encode('utf-8')
                 txn.put(handle, tag.serialize())
             self.tags[tag_name] = handle
         return self.tags[tag_name]
@@ -819,7 +851,9 @@ def gramps_upgrade_14(self):
         new_note = (handle, gramps_id, styled_text, format, note_type,
                     change, marker, private)
         with BSDDBTxn(self.env, self.note_map) as txn:
-            txn.put(str(handle), new_note)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_note)
         self.update()
 
     # ---------------------------------
@@ -839,7 +873,9 @@ def gramps_upgrade_14(self):
                      description, place, new_source_list, note_list, 
                      new_media_list, new_attribute_list, change,marker,private)
         with BSDDBTxn(self.env, self.event_map) as txn:
-            txn.put(str(handle), new_event)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_event)
         self.update()
 
     # ---------------------------------
@@ -921,7 +957,9 @@ def gramps_upgrade_14(self):
                       )
 
         with BSDDBTxn(self.env, self.person_map) as txn:
-            txn.put(str(handle), new_person)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_person)
         self.update()
 
     # ---------------------------------
@@ -953,7 +991,9 @@ def gramps_upgrade_14(self):
                       change, marker, private)
 
         with BSDDBTxn(self.env, self.family_map) as txn:
-            txn.put(str(handle), new_family)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_family)
         self.update()
 
     # ---------------------------------
@@ -978,7 +1018,9 @@ def gramps_upgrade_14(self):
                           new_address_list, urls, change, marker, private)
 
         with BSDDBTxn(self.env, self.repository_map) as txn:
-            txn.put(str(handle), new_repository)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_repository)
         self.update()
 
     # ---------------------------------
@@ -996,7 +1038,9 @@ def gramps_upgrade_14(self):
                      new_date, marker, private)
 
         with BSDDBTxn(self.env, self.media_map) as txn:
-            txn.put(str(handle), new_media)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_media)
         self.update()
 
     # ---------------------------------
@@ -1014,7 +1058,9 @@ def gramps_upgrade_14(self):
                      new_source_list, note_list, change, marker, private) 
 
         with BSDDBTxn(self.env, self.place_map) as txn:
-            txn.put(str(handle), new_place)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_place)
         self.update()
 
     # ---------------------------------
@@ -1033,7 +1079,9 @@ def gramps_upgrade_14(self):
                       marker, private)
 
         with BSDDBTxn(self.env, self.source_map) as txn:
-            txn.put(str(handle), new_source)
+            if isinstance(handle, UNITYPE):
+                handle = handle.encode('utf-8')
+            txn.put(handle, new_source)
         self.update()
 
     # Bump up database version. Separate transaction to save metadata.