I would like to prevent writing when there is the same document ID in firestoreb.
I am new to flutter.
As the title says, when writing from flutter to firestore, if the same document ID exists, it is usually overwritten.
So, I would like to prevent the same document ID from being overwritten.
Is there a good way to do this?
>Solution :
The most straightforward way is to use a security rule as follows:
match /kyousyoku/{docId} {
allow write: if !exists(/databases/$(database)/documents/kyousyoku/$(docId))
}
A small drawback of this approach is the fact that the error message sent back to the client doesn’t indicate that the write has failed because the document already exists.
If you want to be able to make the distinction between this error and other errors returned by the security rules (e.g. the user is not authenticated) you can use a Transaction.
