A listing tool has a "create listing" step. It sends a new product to a marketplace under an item ID the seller chooses. The marketplace call behind that step is not a create. It is a full PUT, keyed by the seller's own item ID, that creates the listing if the ID is new and replaces it if the ID already exists. Twice, the ID already existed.
Key facts
- The "create" step was a full PUT upsert keyed by the seller's own item ID.
- The item ID box kept the last typed value between scans. A new product went out under the ID of a live listing with 89 units in stock. The PUT re-pointed that listing to the new product and replaced all its attributes. At shipment time the marketplace said the listing had no active offer.
- A "reuse an old ID" shortcut sent the same full PUT to a variation child. The PUT carried no size and no parent link, so the child lost its size and could not link to its parent.
- The seller spent over an hour restoring it, and the restore set the price to $300 instead of $249.95.
- Nothing was deleted. The write itself was the damage.
What happened
The first case started with a form. The seller scans a product, types an item ID, and sends it. The item ID box did not clear after a send, so on the next scan it still held the last value. A new product went out under the ID of a listing that was live, with 89 units in stock. The marketplace did what a PUT does: it replaced the record at that key. The live listing now described the new product, with all of its attributes overwritten. Nobody saw it until shipment, when the marketplace said the listing had no active offer.
The second case came through a shortcut meant to save typing: reuse an old ID. It sent the same full PUT, this time to a listing that was a variation child, one size of a product with several. The full record the tool built had no size and no link to the parent. The marketplace took the PUT and then reported that the child was missing its size and could not link to its parent. Restoring it by hand took over an hour, and the restored price came back as $300 instead of $249.95.
Why it looked safe
The step was called "create", so everyone read it as one. Every test used a fresh ID, and with a fresh ID an upsert behaves exactly like a create. The marketplace answered success for the overwrite too, because from its side nothing went wrong: it was asked to replace a record and it did.
The rule: read before you write
When the key can come from a person or from a reused value, look it up before you send anything. There are three outcomes, and each has one answer:
- The key does not exist. Create the record.
- It exists for the same thing. Patch only the fields you own, for example the offer: price and quantity. Never resend the full record.
- It exists for a different thing. Refuse, send nothing, and say so plainly: "This item ID is already used by another listing. Nothing was sent."
The fix
Before
After
In code, the check is a few lines in front of the write:
existing = get(key)
if existing is none:
create(key, record)
elif sameThing(existing, record):
patch(key, ownedFields(record))
else:
refuse("This item ID is already used by another listing. Nothing was sent.")
log("write", key, existing, record, action)
The comparison in sameThing should use what makes the product that product: a barcode, a parent product, the variation it belongs to. A title is not enough, because titles get edited.
Clear the key after each use
A box that keeps its last value is a reused key that nobody chose. Clear user-typed keys after each send. If keeping the value is a feature, show plainly that it will be reused, next to the button, before the person presses it. A "reuse an old ID" shortcut is fine, but it must go through the same read as everything else. A shortcut that skips the check is where the check matters most.
How to find these spots
Look for writes that replace whatever is at a key: an HTTP PUT, a document set() without merge, an upsert, INSERT ... ON DUPLICATE KEY UPDATE, INSERT OR REPLACE, an object storage PUT. A first pass:
grep -rnE "PUT|\.set\(|upsert|ON DUPLICATE KEY|INSERT OR REPLACE" src/
Then, for each hit, ask where the key comes from. A key your own system generates is usually safe. A key from a form field, a scan, a cache, a URL, a "last used" value or a recycled list is the one to guard. Those are the spots where a person can point the write at the wrong record.
What to log
One line per write: the key, what was there before, what you meant to put there, and the action taken: created, patched or refused. With that line, an overwrite shows up as "existing target A, new target B" instead of a quiet success. It also gives you the old state, which matters when you need to put it back.
Why a delete guard does not catch this
Most safety checks watch deletes: a confirm dialog, a soft delete, an alert on removed rows. An overwrite deletes nothing. The record count stays the same, every delete check passes, and an audit for removed rows finds none. The old record is gone in content, not in count. Guard the write itself.
Common questions
Is PUT always an upsert?
By its HTTP meaning, PUT replaces the whole resource at that key. Check the API's own docs: many treat it as create or replace, which is the case that bites here.
Isn't the read a race?
It can be. If the API has a conditional write, such as an if-match header, a version number or a create-only flag, use it as well. The read still catches the person's mistake, which is the common case.
What about restoring after an overwrite?
Keep the previous state from your write log, so a restore is a replay and not retyping. Here the restore was retyped, and the price came back wrong.
Melta is free; get it on the App Store or Google Play.