-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique constraints appearantly not checked when updating document #151
Comments
I suspect the error to be in DataService.java:186:
I think it should be:
|
I could not reproduce this issue. Here is a small test @Test
public void testIssue151() {
Document doc1 = new Document().put("id", "test-1").put("fruit", "Apple");
Document doc2 = new Document().put("id", "test-2").put("fruit", "Ôrange");
NitriteCollection coll = db.getCollection("test");
coll.insert(doc1, doc2);
coll.createIndex("fruit", indexOptions(IndexType.Unique));
assertEquals(coll.find(eq("fruit", "Apple")).totalCount(), 1);
Document doc3 = new Document(coll.find(eq("id", "test-2")).firstOrDefault());
doc3.put("fruit", "Apple");
coll.update(doc3);
assertEquals(coll.find(eq("fruit", "Apple")).totalCount(), 2);
} I am getting below valid exception
Probably you have missed this Document doc3 = new Document(coll.find(eq("id", "test-2")).firstOrDefault()); Unless you copy it to a new document, any modification will be in-place. Even if you don't call update on collection, the document object will be updated. On a second thought, I can make a small change, so that you will get a cloned document instead of the actual one and collection will be protected from this kind of mistakes. |
Now this fix is available in 3.3.0-SNAPSHOT. |
Thanks for picking this up and making the interface easier to use. I see your point with the necessity to clone the item before modifying it. I was indeed unter the impression that any "get" operation provides me with a clone, thus the whole point for an "update" operation. |
Given a
NitriteCollection
labeledlist
exists, for which there is a unique index for field "Data"Given a document labeled
1
exists in the collectionlist
with field "Data" = "test"Given another document labeled
2
exists in the collectionlist
with field "Data" = "test2"[ { "_id": 1, "Data": "test"}, { "_id": 2, "Data": "test2"} ]
Reproduction steps:
2
would be updated so that "Data" changes to "test" knowing that the same value already exists on1
and would violate the unique constraint. This would be achived by fetching2
from the collection, setting "Data" totest
and attempting to callupdate
on the collection to store the changes.Expectation: an exception is raised similar to a scenario where a third document with "Data" = "test" would be inserted.
Observation: no exception is raised. The change is stored and now the value "test" is not unique anymore within
list
- despite the unique constraint.The text was updated successfully, but these errors were encountered: