admin管理员组文章数量:1302404
I am working with React 16.3.2, Redux 4 and Dexie 2.0.3.
when I am going to store data second time it throws this error message.
Error: ConstraintError: Key already exists in the object store.
return dispatch => {
db.table
.add(data)
.then (function(id){
console.log(id)
})
.catch (function (error) {
console.log("Error: " + error);
});
}
My Db schema:
const db = new Dexie('ReactReduxDexieJsCRUD');
db.version(1).stores({table:'++id,name,age,bloodGroup,donateBefore,weight' });
The first time it stores date well but after it gives the error.
I am working with React 16.3.2, Redux 4 and Dexie 2.0.3.
when I am going to store data second time it throws this error message.
Error: ConstraintError: Key already exists in the object store.
return dispatch => {
db.table
.add(data)
.then (function(id){
console.log(id)
})
.catch (function (error) {
console.log("Error: " + error);
});
}
My Db schema:
const db = new Dexie('ReactReduxDexieJsCRUD');
db.version(1).stores({table:'++id,name,age,bloodGroup,donateBefore,weight' });
The first time it stores date well but after it gives the error.
Share edited May 18, 2018 at 11:12 MD Ashik asked May 17, 2018 at 18:25 MD AshikMD Ashik 9,84510 gold badges55 silver badges61 bronze badges 8-
2
Try
put(data)
instead ofadd(data)
. – Oblosys Commented May 17, 2018 at 18:59 -
I didn't worked with Dexie, but similar problem on MySql can be solved with "ON DUPLICATE KEY UPDATE". If you want on second call to add new data as new record, you need to set table key to be autoincrement, or to specify new key manually as second parameter to
add
call. If you want to add data or update data if exist, try [dexie/docs/Table/Table.put()](put) instead ofadd
. – stolex Commented May 17, 2018 at 18:59 - Lovely ment #Oblosys & #stolex but it creates another issue it doesn't change the ID and it works like #update in database – MD Ashik Commented May 17, 2018 at 19:04
- works as expected, but you dont want an update, so what do you actually want to happen when a duplicate key is used? – user9487972 Commented May 17, 2018 at 22:51
- @smith I don't want to use the duplicate key. and why it gives me error duplicate key . i am submitted my from second time with new value so it should be store new data with new ID ? – MD Ashik Commented May 18, 2018 at 9:05
1 Answer
Reset to default 8How does your schema look like? (the part db.version(x).stores({...}) ?
The most mon is to have inbound primary key, example:
db.version(1).stores({
table: 'id, idx1, idx2...'
});
Here id
is the primary key.
db.table.add({id: 1, foo: 'bar'})
will add object with id 1.db.table.add({id: 1, foo: 'bar2'})
2nd time will fail because id 1 exists.db.table.put({id: 1, foo: 'bar2'})
will update object with id 1.
So what do you really want to do? You say you want to add new object with new key. If so, I suppose the error is that you give the same key second time.
You can also let the id be generated by the db
db.version(2).stores({
table: '++id, idx1, idx2...'
});
Then you don't need to supply id in calls to add():
db.table.add({foo: 'bar'})
will add object with id 1.db.table.add({foo: 'barX'})
2nd time will add new obj with id 2- ...
本文标签: javascriptConstraintError Key already exists in the object storeStack Overflow
版权声明:本文标题:javascript - ConstraintError: Key already exists in the object store - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741692936a2392837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论