diff --git a/repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryDataShard.java b/repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryDataShard.java index e8a8d1cf..785d8ead 100644 --- a/repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryDataShard.java +++ b/repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryDataShard.java @@ -161,12 +161,13 @@ private void validateUniqueness(long txId, long version, T entity) { .toList(); for (Schema.Index index : indexes) { Map entityIndexValues = buildIndexValues(index, entity); - for (InMemoryEntityLine line : entityLines.values()) { + entityLines.forEach((id, line) -> { Columns columns = line.get(txId, version); - if (columns != null && entityIndexValues.equals(buildIndexValues(index, columns.toSchema(schema)))) { + if (columns != null && !id.equals(entity.getId()) + && entityIndexValues.equals(buildIndexValues(index, columns.toSchema(schema)))) { throw new EntityAlreadyExistsException("Entity " + entity.getId() + " already exists"); } - } + }); } } diff --git a/repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java b/repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java index 3356ad42..527f931e 100644 --- a/repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java +++ b/repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java @@ -1355,10 +1355,12 @@ private void findInKeysViewFilteredAndOrdered(Set keys, boole @Test public void testUniqueIndex() { - UniqueProject ue1 = new UniqueProject(new UniqueProject.Id("id1"), "valuableName"); + String verySameName = "valuableName"; + UniqueProject ue1 = new UniqueProject(new UniqueProject.Id("id1"), verySameName, 1); db.tx(() -> db.table(UniqueProject.class).save(ue1)); - UniqueProject ue2 = new UniqueProject(new UniqueProject.Id("id2"), "valuableName"); - assertThrows(EntityAlreadyExistsException.class, () -> db.tx(() -> db.table(UniqueProject.class).insert(ue2))); + db.tx(() -> db.table(UniqueProject.class).save(ue1).withVersion(2)); // no exception + UniqueProject ue2 = new UniqueProject(new UniqueProject.Id("id2"), verySameName, 1); + assertThrows(EntityAlreadyExistsException.class, () -> db.tx(() -> db.table(UniqueProject.class).save(ue2))); } @Test diff --git a/repository-test/src/main/java/tech/ydb/yoj/repository/test/sample/model/UniqueProject.java b/repository-test/src/main/java/tech/ydb/yoj/repository/test/sample/model/UniqueProject.java index a3037eae..ae258e19 100644 --- a/repository-test/src/main/java/tech/ydb/yoj/repository/test/sample/model/UniqueProject.java +++ b/repository-test/src/main/java/tech/ydb/yoj/repository/test/sample/model/UniqueProject.java @@ -11,6 +11,8 @@ public class UniqueProject implements Entity { Id id; @With String name; + @With + int version; @Value public static class Id implements Entity.Id {