From ef60fa519c30c5b8450da36151dac9c6b9457549 Mon Sep 17 00:00:00 2001 From: Anton Golubev Date: Mon, 28 Aug 2023 14:03:11 +0300 Subject: [PATCH] Ensure Model ID is unique The ID of the newly created model was obtained as the length of the array of models, but if some models "in the middle" were deleted, the new ID could be the same as the existing one. We are sure that the last ID is the maximum, so we just take +1 than the last element. --- howdy/src/cli/add.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/howdy/src/cli/add.py b/howdy/src/cli/add.py index 7a6d9ec..8baa0f1 100644 --- a/howdy/src/cli/add.py +++ b/howdy/src/cli/add.py @@ -78,13 +78,16 @@ if not builtins.howdy_args.plain: # Set the default label label = "Initial model" +# some id's can be skipped, but the last id is always the maximum +next_id = encodings[-1]["id"] + 1 if encodings else 0 + # Get the label from the cli arguments if provided if builtins.howdy_args.arguments: label = builtins.howdy_args.arguments[0] -# If models already exist, set that default label -elif encodings: - label = _("Model #") + str(len(encodings) + 1) +# Or set the default label +else: + label = _("Model #") + str(next_id) # Keep de default name if we can't ask questions if builtins.howdy_args.y: @@ -106,7 +109,7 @@ if "," in label: insert_model = { "time": int(time.time()), "label": label, - "id": len(encodings), + "id": next_id, "data": [] }