Error while adding new record without explicit key (using auto inc key instead)

huangapple go评论51阅读模式
英文:

Error while adding new record without explicit key (using auto inc key instead)

问题

Using the idb 包,我试图向我的应用程序数据库添加新记录。对象存储创建如下:

const dbEditorStore = db.createObjectStore("myStore",
    { keyPath: "id", autoIncrement: true });
dbEditorStore.createIndex("resultIndex", "resultId", { unique: false });
dbEditorStore.createIndex("tabIndex", "tabId", { unique: false });

当我尝试添加新数据时,没有明确提供键,我会收到错误消息

ConstraintError: Key already exists in the object store.

这是我添加数据的方式(请注意缺少键参数):

await db.add("myStore", data);

这让我感到困惑,因为我期望对于自动递增键,我不需要指定任何内容,IndexedDB 应该会自动处理。

这里有什么问题,以及在这种情况下添加数据的正确方法是什么?

更多信息:当我在 add 调用中指定键名时,我会收到不同的错误消息:

DataError: Failed to execute 'add' on 'IDBObjectStore': The object store uses in-line keys and the key parameter was provided.

英文:

Using the idb package I'm trying to add new records to my application DB. The object store is created like this:

const dbEditorStore = db.createObjectStore("myStore",
    { keyPath: "id", autoIncrement: true });
dbEditorStore.createIndex("resultIndex", "resultId", { unique: false });
    dbEditorStore.createIndex("tabIndex", "tabId", { unique: false });

When I now try to add new data, without explicitly giving a key I get the error

> ConstraintError: Key already exists in the object store.

Here's how I add the data (note the missing key parameter):

await db.add("myStore", data);

which confuses me as I expect that for an auto increment key I don't have to specify anything and IndexedDB would handle that automatically.

What is wrong here and what is the correct way to add data in this scenario?

Further info: when I specify the key name in the add call I get a different error:

> DataError: Failed to execute 'add' on 'IDBObjectStore': The object store uses in-line keys and the key parameter was provided.

答案1

得分: 1

以下是翻译好的部分:

一个显然与在插入数据时未满足的条件无关的答案给了我一个最终的想法,究竟是什么出了问题。当指定keyPath值时,不会创建单独的主键,而是使用键路径中给定的字段作为主键(内联键)。这一方面在MDN文档中描述得很差:

> 键路径
定义浏览器应从对象存储或索引中提取键的位置。有效的键路径可以包括以下内容:空字符串、JavaScript标识符或由句点分隔的多个JavaScript标识符,或包含其中任何一个的数组。它不能包含空格。

什么是“键”,如何从对象存储中提取键?

无论如何,在这里的解决方案是根本不指定键:

const dbEditorStore = db.createObjectStore("myStore", { autoIncrement: true });

但只指定键生成器(autoIncrement)。我假设keyPath的值将是自动值(因为如果它引用数据中的字段,它不能自动更改)。

英文:

An apparently unrelated answer about unmet conditions when inserting data gave me the ultimate idea what was wrong. When specifying the keyPath value no separate primary key is created, but the field given in the key path is used for the primary key (inline key). This aspect is badly described in the MDN documentation:

> key path
Defines where the browser should extract the key from in the object store or index. A valid key path can include one of the following: an empty string, a JavaScript identifier, or multiple JavaScript identifiers separated by periods or an array containing any of those. It cannot include spaces.

What is "the key" and how can a key be extracted from an object store?

Anyway, the solution here is to not specify a key at all:

const dbEditorStore = db.createObjectStore("myStore", { autoIncrement: true });

but only the key generator (autoIncrement). I assumed the value keyPath would be an automatic value (since it cannot be automatically changed, if it were referring to a field in the data).

huangapple
  • 本文由 发表于 2023年4月13日 16:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定