mongoid/mongoid — Issue #1674: Mongoid::Paranoia has unintended side-effects on document lifecycle
Author: ssimeonov
Created: 2012-02-04 23:24:35+00:00
State: open

When a paranoid document with a composite unique key (managed by MongoDB) is deleted and then an attempt is made to create a document with the same composite key--voilating the uniqueness constraint in MongoDB--`ExampleModel.create!` would succeed but the document won't be persisted. `ExampleModel.reload` would fail, for example.

This behavior is undocumented and smells like a bug. The semantics of `create!` are such that if it doesn't raise an error one should be able to trust the instance that comes back.

There are two possible behaviors that would be semantically valid:

1. `create!` should raise an error that is descriptive-enough for a developer to understand that the issue has to do with a live paranoid document.

2. `Mongoid::Paranoia` should offer the option to allow new documents to be created that would override existing documents. In other words, it would detect a uniqueness constraint violation and automatically call `delete!` (or `destroy!`) and then retry the document creation.