{"record":{"id":"398db3a0df98e463","repo":"quarkusio/quarkus","slug":"there-should-be-only-one-result-398db3","errorCode":null,"errorMessage":"There should be only one result","messagePattern":"There should be only one result","errorType":"exception","errorClass":"PanacheQueryException","httpStatus":null,"severity":"error","filePath":"extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java","lineNumber":225,"sourceCode":"\n    @SuppressWarnings(\"unchecked\")\n    public <T extends Entity> Stream<T> stream() {\n        return (Stream<T>) list().stream();\n    }\n\n    public <T extends Entity> T firstResult() {\n        List<T> list = list(1);\n        return list.isEmpty() ? null : list.get(0);\n    }\n\n    public <T extends Entity> Optional<T> firstResultOptional() {\n        return Optional.ofNullable(firstResult());\n    }\n\n    public <T extends Entity> T singleResult() {\n        List<T> list = list(2);\n        if (list.size() != 1) {\n            throw new PanacheQueryException(\"There should be only one result\");\n        }\n\n        return list.get(0);\n    }\n\n    public <T extends Entity> Optional<T> singleResultOptional() {\n        List<T> list = list(2);\n        if (list.size() > 1) {\n            throw new PanacheQueryException(\"There should be no more than one result\");\n        }\n\n        return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));\n    }\n\n    private void manageOffsets(FindIterable find, Integer limit) {\n        if (range != null) {\n            find.skip(range.getStartIndex());\n            if (limit == null) {","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/mongodb-panache-common/runtime/src/main/java/io/quarkus/mongodb/panache/common/runtime/CommonPanacheQueryImpl.java#L207-L243","documentation":"Panache's singleResult() expects exactly one row from the executed MongoDB query. The library fetches up to 2 rows (list(2)) and throws PanacheQueryException if the result count is not exactly 1 — i.e. zero rows or more than one row both trigger it.","triggerScenarios":"Calling query.singleResult() when the query matches no documents, or when the query (filter/sort) matches two or more documents.","commonSituations":"Forgetting a filter so an entity query matches every document; unique constraints assumed but not enforced in MongoDB; stale data left from earlier test runs.","solutions":["Check the query matches exactly one document; add/fix the filter before calling singleResult().","If zero rows is acceptable, use singleResultOptional() instead.","If only the first row is needed regardless of count, use firstResult().","Delete duplicate documents in the collection or add an application-level uniqueness check."],"exampleFix":"// before\nMyEntity e = query.singleResult(); // throws when 0 or >1 rows\n// after\nOptional<MyEntity> e = query.singleResultOptional(); // tolerates 0 rows\n// or\nMyEntity e = query.firstResult(); // takes first row only","handlingStrategy":"try-catch","validationCode":"long count = query.count();\nif (count != 1) {\n    // handle zero/multiple rows before calling singleResult()\n}","typeGuard":null,"tryCatchPattern":"try {\n    MyEntity e = query.singleResult();\n} catch (PanacheQueryException e) {\n    // fall back to firstResult() or Optional handling\n}","preventionTips":["Query by unique keys (_id, unique-indexed fields) when using singleResult().","Prefer singleResultOptional() when zero rows is a valid outcome.","Prefer firstResult() when any single row is acceptable.","Enforce uniqueness at write time so duplicate matches cannot occur."],"tags":["mongodb","panache","query-result-count"],"backgroundTag":"query-result-count-mismatch","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}