From 0d8494a44bab74176c29d332b85d77f30e1759a6 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Thu, 11 Apr 2024 10:49:00 +0000 Subject: [PATCH] Only set `last_insert_id` value if at least one row was inserted/changed. Signed-off-by: Arthur Schreiber --- go/vt/vtgate/engine/insert.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/go/vt/vtgate/engine/insert.go b/go/vt/vtgate/engine/insert.go index 332ccc92098..28cd18fa2be 100644 --- a/go/vt/vtgate/engine/insert.go +++ b/go/vt/vtgate/engine/insert.go @@ -174,9 +174,16 @@ func (ins *Insert) executeInsertQueries( return nil, vterrors.Aggregate(errs) } + // If this insert used auto increment values from a sequence, we need to set the `last_insert_id` value. if insertID != 0 { - result.InsertID = insertID + // If no rows were inserted or updated, clear the `last_insert_id` value. + if result.RowsAffected > 0 { + result.InsertID = insertID + } else { + result.InsertID = 0 + } } + return result, nil }