Firestore Reference

Viewed 134

I use the Cloud Firestore database and have linked some entries of a collection by reference.

Now I have the problem, if I delete the reference document, the reference is still there, but the referenced document is not anymore.

What is the plan here, is this a wrong approach for the reference?

Article {
   name: string;
   price: number;
}

OrderEntry {
   name: string;
   article: Reference;
}

If I now delete an article, the reference is still present on the OrderEntry, but the article no longer exists.

When deleting an item, can I check if it is referenced in another entity?

1 Answers

The only way to accomplish this is to make a query for documents with a known field that can contain the reference to the deleted document. Firestore will not do this for you - it doesn't care if a document with existing references is deleted.

You can query for references just like any other query in Firestore. Just pass a DocumentReference object as the value of the field that could contain its value.

Related