scottwrobinson / camo

A class-based ES6 ODM for Mongo-like databases.
556 stars 80 forks source link

Don't allow to erase documents if they are being referenced in other documents. #47

Closed michaeljota closed 8 years ago

michaeljota commented 8 years ago

If you delete a document, the referenced id of the other document becomes null, and that's if you save the document with the referenced id.

This is a unexpected behavior. I think it should throw an error to show that's being used.

royaltm commented 8 years ago

I don't think it would be easy to implement because mongo/nedb doesn't support foreign key concepts. These are not relational databases. They are document databases. IMHO the exact behavior as it is, is actually expected. The relation between documents is just a hack in camo. It's your own responsibility to maintain database integrity. One would need to iterate over all collections containing references to a collection of the document being deleted and query each collection for count of references to the deleted document. Alternative approach would be to store reference counter in the referenced document and quick check it before deletion. But even then it wouldn't stop you to connect to db back-end directly and mess with your relations, because db engine doesn't care about them.

michaeljota commented 8 years ago

Oh! Ok. I did not know that. Thanks! I'll close this.