diff --git a/product/ERP5/Tool/IdTool.py b/product/ERP5/Tool/IdTool.py
index 10dd68f28fdb9a547a2e366b142576d6a4b60d57..6fbcb6d1b2347096aefe018263f82ad56b1e2bd9 100644
--- a/product/ERP5/Tool/IdTool.py
+++ b/product/ERP5/Tool/IdTool.py
@@ -192,6 +192,11 @@ class IdTool(UniqueObject, Folder):
       id_group = repr(id_group)
     if not isinstance(default, int):
       default = 1
+    if self.dict_length_ids.get(id_group) is None:
+      self.dict_length_ids[id_group] = Length()
+    # If the table has been flushed, the latest stored id must be used if
+    # it's greater than the default value.
+    default = max(default, self.dict_length_ids[id_group]())
     # FIXME: A skin folder should be used to contain ZSQLMethods instead of
     # default catalog, like activity tool (anyway, it uses activity tool
     # ZSQLConnection, so hot reindexing is not helping here).
@@ -207,8 +212,6 @@ class IdTool(UniqueObject, Folder):
     finally:
       commit()
     new_id = result[0]['LAST_INSERT_ID()']
-    if self.dict_length_ids.get(id_group) is None:
-      self.dict_length_ids[id_group] = Length(new_id)
     self.dict_length_ids[id_group].set(new_id)
     return range(new_id - id_count, new_id)