Unique constraint failed error - 2067 with pouchdb-mapreduce- Android
SuhailMehta opened this issue · 9 comments
I am working on react-native project which uses sync so we decided to use couch-pouch. With pouchdb websql adapter i am using your your sqlite storage which is working fine for ios app but when we try to run the same app on android its was able to insert data from couch to sqlite storage but when we query the data it throws an error pointing Unique constraint failed for document-store.id (error - 2067).
With pouchdb-mapreduce i can see the _pouch_dbName-mrview db being created but it was empty.
So, before query the indexes insert must be done for a view but as db is empty i can't under why unique constraints are failing.
Can you suggest the a way to resolve it or i can add some more information to it.
Unique constraint errors have nothing to do with this library, this is SQL related. Check the database schema and especially all unique indexes and primary keys. There should be the root cause of your exception
I am understanding what you are saying but the thing is same js wrapper is working fine on ios but only throwing error for android on same data set and same query execution.
i think i got the issue. my id field contains \u000 character which was constructed by pouchdb-mapreduce and thats not supported for cordova-sqlite-storage.
My id(unique) field contains 55323246\u0000\u00004Category_6191\u000031\u0000\u0000.
And data after \u0000 gets truncated some how and every time it tries to insert 55323246 and cause unique constraint failed.
https://stash.mcon-group.com/users/ok250343/repos/pouchdb-hello-world/browse/plugins/cordova-sqlite-storage?at=master
@brodybits - Chris hi, is this something that has been identified in the core cordova-storage module and can be fixed?
@SuhailMehta Hi, did you manage it? Seems like pouchdb is heavily using \u0000 for indexing.
It seems that '\u000' is wrongfully interpreted as '\0' on some platforms and thus the issue. I have found some mentions of it on the pouchdb website. Perhaps newer versions of PouchDB shy away from using this character?
I think it's react native-related problem: facebook/react-native#12731
@noradaiko I have change '\u0000' in pouchdb to '\u0001' in pouchdb internals. I think if you will dig inside sql adapter or if you will read its(sql adapter) read me you have to replace '\u0000' to '\u0001' for some platforms (android platform for me).
Actually code is written to replace the same in pouchdb-adapter-websql-core but it was not working.
So, i change var zero = '\u0001' in pouchdb-collate and everything is working fine for then.
@SuhailMehta Thanks for the answer.
The code replacing '\u0000' in pouchdb-adapter-websql-core works only for blobs(attachments) so it won't fix the problem.
I'm afraid that it corrupts data if I change internal pouchdb.
Instead, I made react-native-sqlite-2 which code base is from Nolan's cordova-plugin-sqlite-2 with my patch for this problem, and it works pretty well on both Android and iOS for now.
Check it out if you are interested in.