[w3c/IndexedDB] In "object store storage operation", be more explicit about how to handle ConstraintError from an index (Issue #477)

dumbmatter created an issue (w3c/IndexedDB#477)

In the [object store storage operation](https://w3c.github.io/IndexedDB/#object-store-storage-operation), steps 5-3 and 5-4 both cover situations where an index can result in a ConstraintError as the result of the operation:

> 5-3. If index’s [multiEntry flag](https://w3c.github.io/IndexedDB/#index-multientry-flag) is false, or if index key is not an [array key](https://w3c.github.io/IndexedDB/#array-key), and if index already contains a [record](https://w3c.github.io/IndexedDB/#object-store-record) with [key](https://w3c.github.io/IndexedDB/#key) [equal to](https://w3c.github.io/IndexedDB/#equal-to) index key, and index’s [unique flag](https://w3c.github.io/IndexedDB/#index-unique-flag) is true, then this operation failed with a "[ConstraintError](https://webidl.spec.whatwg.org/#constrainterror)" [DOMException](https://webidl.spec.whatwg.org/#idl-DOMException). Abort this algorithm without taking any further steps.

> 5-4. If index’s [multiEntry flag](https://w3c.github.io/IndexedDB/#index-multientry-flag) is true and index key is an [array key](https://w3c.github.io/IndexedDB/#array-key), and if index already contains a [record](https://w3c.github.io/IndexedDB/#object-store-record) with [key](https://w3c.github.io/IndexedDB/#key) [equal to](https://w3c.github.io/IndexedDB/#equal-to) any of the [subkeys](https://w3c.github.io/IndexedDB/#subkeys) of index key, and index’s [unique flag](https://w3c.github.io/IndexedDB/#index-unique-flag) is true, then this operation failed with a "[ConstraintError](https://webidl.spec.whatwg.org/#constrainterror)" [DOMException](https://webidl.spec.whatwg.org/#idl-DOMException). Abort this algorithm without taking any further steps.

They both end with "Abort this algorithm without taking any further steps." But in the case where you call `event.preventDefault()` in the request's error handler to prevent the transaction from aborting, that's not enough. You also need to undo any changes made in earlier steps, specifically increasing the key generator's current number (step 1) and storing the record in the object store (step 4). This behavior is reflected in WPT (request-event-ordering tests) and current browsers.

I'm not sure if that is somehow implicit in the phrase "Abort this algorithm without taking any further steps." But if you look elsewhere in the spec like for [aborting a transaction](https://w3c.github.io/IndexedDB/#abort-a-transaction) it is much more explicit about reverting changes:

> All the changes made to the [database](https://w3c.github.io/IndexedDB/#database) by the [transaction](https://w3c.github.io/IndexedDB/#transaction-concept) are reverted. For [upgrade transactions](https://w3c.github.io/IndexedDB/#upgrade-transaction) this includes changes to the set of [object stores](https://w3c.github.io/IndexedDB/#object-store) and [indexes](https://w3c.github.io/IndexedDB/#index-concept), as well as the change to the [version](https://w3c.github.io/IndexedDB/#database-version). Any [object stores](https://w3c.github.io/IndexedDB/#object-store) and [indexes](https://w3c.github.io/IndexedDB/#index-concept) which were created during the transaction are now considered deleted for the purposes of other algorithms.

Maybe similar language should be there for object store storage, like:

> All the changes made to the database by this operation are reverted. This includes changes to the records stored in the object store and the key generator's current number.

Or @nolanlawson [suggested](https://github.com/dumbmatter/fakeIndexedDB/pull/157#issuecomment-3397961912) maybe that language from "abort a transaction" could be refactored into its own algorithm/definition and used in both places.

(I'm posting this because I wrote [fake-indexeddb](https://github.com/dumbmatter/fakeIndexedDB/) mostly by trying to implement all the algorithms in the spec, and I just now realized that [I missed handling this situation](https://github.com/dumbmatter/fakeIndexedDB/pull/157).)

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/IndexedDB/issues/477
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/IndexedDB/issues/477@github.com>

Received on Monday, 13 October 2025 17:25:59 UTC