I'm having problems with this code when I upgrade the version without any other modification, and I can't understand the reason.
function create_registry() {
var version = 1;
var indexeddb_db = "examples"; // database
var indexeddb_table = "examples_table"; // table
// open the database
var indexeddb_request = indexedDB.open( indexeddb_db, version ); // connect + open database
var db = false;
// if error
indexeddb_request.onerror = function ( event ) {
console.log( event.target );
console.trace();
}
// if success
indexeddb_request.onsuccess = function ( event ) {
console.log( event.target );
console.trace();
}
// if onupgradeneeded
indexeddb_request.onupgradeneeded = function( event ) {
console.log( event.target );
console.trace();
db = event.target.result;
var objectStore = db.createObjectStore( indexeddb_table, { keyPath: 'url' } );
}
}
The first time that the page is loaded, the indexedDB object (database) is created and the table inside is created. Everything works fine. First the onupgradeneeded is executed and then the onsuccess is launched.
If I reload the page without any changes, everything works fine, the onsuccess is launched.
But, if I change the version number, then I get the errors mentioned below. Those errors are described briefly in the W3C spec of the Indexed Database API, but that doesn't help me much so far. After the onupgradeneeded is executed, the onerror is execute and there I have an AbortError, but that doesn't tell me much more either.
Chrome 28 "Uncaught Error: ConstraintError: DOM IDBDatabase Exception 0"
Firefox 22 "A mutation operation in the transaction failed because a constraint was not satisfied. For example, an object such as an object store or index already exists and a request attempted to create a new one. "
As far as I can tell, the problem is that I'm trying to recreate the same object with the same keypath, but isn't the new version making the script recreate the whole object?
Why Am I getting the error? Shouldn't the onupgradeneeded just update the version number and rewrite the object (database)?