fix duplicate when updating freshly inserted element

and add associated unit tests