From eb25877cfbad67f622ab94b1745ac85dea47eb0f Mon Sep 17 00:00:00 2001 From: Andrew Davison Date: Thu, 25 Jul 2024 14:31:30 +0200 Subject: [PATCH] more careful existence checks --- fairgraph/kgobject.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/fairgraph/kgobject.py b/fairgraph/kgobject.py index 44a2c7de..072ca677 100644 --- a/fairgraph/kgobject.py +++ b/fairgraph/kgobject.py @@ -514,9 +514,19 @@ def exists(self, client: KGClient) -> bool: client=client, filters=query_filter, ) - instances = client.query(query=query, size=1, scope="any").data + instances = client.query(query=query, size=2, scope="any").data if instances: + if len(instances) > 1: + raise Exception("Existence query is not specific enough") + + # it seems that sometimes the "query" endpoint returns instances + # which the "instances" endpoint doesn't know about, so here we double check that + # the instance can be found + instance = client.instance_from_full_uri(instances[0]["@id"], scope="any") + if instance is None: + return False + self.id = instances[0]["@id"] assert isinstance(self.id, str) save_cache[self.__class__][query_cache_key] = self.id