diff --git a/src/lib/querybuilder/model/graphology/utils.ts b/src/lib/querybuilder/model/graphology/utils.ts
index 5e830e6f40ef9797e153da0e25f86766fb0f4271..5e1adb6641d9af21e9527c614310c7d3c1e8b861 100644
--- a/src/lib/querybuilder/model/graphology/utils.ts
+++ b/src/lib/querybuilder/model/graphology/utils.ts
@@ -51,7 +51,10 @@ export class QueryMultiGraphology extends MultiGraph<QueryGraphNodes, QueryGraph
     attributes.width = width;
     attributes.height = height;
 
-    if (!attributes.id) attributes.id = 'id_' + this.nodes().length;
+    const ids: number[] = this.nodes()
+      .map(x => parseInt(x.slice(3)))
+      .sort((a, b) => a - b);
+    if (!attributes.id) attributes.id = 'id_' + String((ids.at(-1) ?? 0) + 1);
 
     // Add to the beginning the meta attributes, such as (# Connection)
     attributes.attributes = [...checkForMetaAttributes(attributes).map(a => ({ handleData: a })), ...attributes.attributes];