From 5d9c92d0d91a1039a75edb79c722ec3a6aa91df1 Mon Sep 17 00:00:00 2001
From: "Ruben S. Montero" <rsmontero@opennebula.systems>
Date: Mon, 21 Jan 2019 17:58:05 +0100
Subject: [PATCH] development: Fix pool index when the first insert fails

---
 src/pool/PoolSQL.cc | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/pool/PoolSQL.cc b/src/pool/PoolSQL.cc
index 0b89478a2d..392cf6e4f8 100644
--- a/src/pool/PoolSQL.cc
+++ b/src/pool/PoolSQL.cc
@@ -148,7 +148,14 @@ int PoolSQL::allocate(PoolObjectSQL *objsql, string& error_str)
 
     if( rc == -1 )
     {
-        _set_lastOID(--lastOID, db, table);
+        lastOID = lastOID - 1;
+
+        if ( lastOID < 0 )
+        {
+            lastOID = 0;
+        }
+
+        _set_lastOID(lastOID, db, table);
     }
 
     unlock();