Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

ConstraintError: Unable to add key to index 'nameNative': at least one key does not satisfy the uniqueness requirements.

Open sustained opened this issue 8 years ago • 3 comments

Brand new to the library here. I have been reading the documentation for a while and fiddling with this for some time now. No idea what is wrong?

I had this -

db.version(1).stores({
	languages: '++id,identifier,&name,&nameNative',
	courses: '++id,name,languageFrom,languageTo',
	vocabulary: '++id,courseId,source,destination'
});

And it just throws a ConstraintError when I tried to call this function -

async function seedDatabase() {
	await db.languages.add({identifier: 'en', name: 'English', nameNative: 'English'});
	await db.languages.add({identifier: 'sv', name: 'Swedish', nameNative: 'Svenska'});
	await db.courses.add({name: 'Swedish', languageFrom: 'en', languageTo: 'sv'});
	await db.vocabulary.add({courseId: 1, source: 'a man',   destination: 'en man'});
	await db.vocabulary.add({courseId: 1, source: 'a woman', destination: 'en kvinna'});
};

But -

  • With the name key, English and Swedish are definitely both unique... no?
  • With the nameNative key, English and Svenska are definitely both unique... no?

I tried this without the & and the problem persists.

Here's the full error -

image

sustained avatar Sep 12 '17 23:09 sustained

Okay, at some point in my fumbling around it appears to have actually created the database and (mostly) populated it.

Both languages were added, the course was added but only the first vocabulary item was added. If I try to add a second one now, I get the same old error.

This library/IndexedDB doesn't really make much sense to me?

sustained avatar Sep 13 '17 00:09 sustained

Hmm, I deleted all the databases via the dev-tools and then I changed to using bulkAdd and I've since had no problems. Any ideas on why this happened?

sustained avatar Sep 13 '17 01:09 sustained

我看了,你这里应该是想 复合索引唯一,那么不是 languages: '++id,identifier,&name,&nameNative', 而是应该是languages: '++id,identifier,name,nameNative,&[name+nameNative]', 这样可以控制 name+nameName 唯一。

AreYReady avatar Nov 24 '21 09:11 AreYReady