Say multiple users are filling a form at the same time. So when an id (eg, AO001) is generated, incrementing on the last one, duplicates are avoided and if there’s a duplicate in the database, add +1 to it and check again, and then save. There’s an example by Yisrael here https://www.wix.com/corvid/forum/tips-tutorials-examples/example-no-database-duplicates , but this also doesn’t avoid duplicates in case of multiple users filling at the same time.
Any help is appreciated.
Have similar issue I wrote here. https://www.wix.com/velo/forum/community-discussion/atomicity-when-updating-a-field-in-collection
No response received. I think Wix currently doesn’t provide solution for this issue yet.
Yes, there is a solution to this, we discussed this on another forum. Basically, you (mis)use the primary key (the _id). Wix-data will not allow duplicate _id’s and will generate an error. So what you do is this:
- keep track of a last used number in a separate collection or whatever (like your A001)
- any new “insert” reads last number and increment it by 1
- write the incremented number to that collection (so other will not use this number)
- you SUBSTITUTE _id (usually self generated if not provided, like la46-w0987w-jfh4ref etc) by your own id e.g. A004 (this will need wix-data, not dataset) before inserting into collection
- you check for duplicate error (if already exist). If so, repeat 2 and 3, try again.
As a result, all your items in this collection will now carry your self generates _id (A005,6,7,8, etc), not the long id’s generated by Wix. And they will be unique, no dups possible.
this makes total sense. But on step 5 how/where can we catch the error?