firebase / firebase/firebase-tools
Firestore emulator transaction fails if document existed in the past
- Dominant language
- TypeScript
- Stars
- 4.5k
- Forks
- 1.3k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 84
Description
### [REQUIRED] Environment info
**firebase-tools:** 11.0.1
**Platform:** Windows 11
### [REQUIRED] Test case
```
await FirebaseFirestore.instance.runTransaction(
(transaction) {
return transaction.get(reportDocRef).then((reportDoc) {
if (reportDoc.exists) {
if (reportDoc.data()!['processed'] != true) {
transaction.update(reportDocRef, reportMap);
}
} else {
transaction.set(reportDocRef, reportMap);
}
});
},
);
```
This is a simple Firestore transaction running with Flutter:
1. First run -> creates a new document since none exists at the referenced path
2. Second run -> document exists so it checks if field 'processed' is not set and if so writes an update, otherwise doesn't write/update anything.
Everything runs fine until I delete the document, either manually through the console or using the admin SDK. At this point I would assume this takes us back to step 1 but instead the transaction fails with the following error:
> [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: [cloud_firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.
Somehow deleting a document is not equivalent to not having that document to begin with.
This only happen on client SDK with the emulator suite according to my tests, not the admin SDK. I switched to an actual firebase project instead of the emulator suite and tried the same code again, this time it works fine without raising any errors, so it creates the document again after deletion as I expected in a real project, however it is worth mentioning after deletion I cannot see the document getting created again unless I refresh the console (F5).
Contributor guide
Research direction
Start with the provided Flutter transaction against the Firestore emulator: create the document, run the transaction again, delete it through the console or Admin SDK, and retry. Compare this with the Admin SDK and a real Firebase project; done means the client transaction can recreate the deleted document without the failed-precondition error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flutter, typescript
- Domain
- databases, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100