Fix embarrassing bug.

See title. Logic here is supposed to be "while the item id exists, choose a new item id and test again".

Edit: fixes #9685