gsVAM
August 10, 2023, 12:54pm
1
Question that I am very surprised I cannot find an answer to –
Does Weaviate allow a class to have a property that is its own class (i.e. a self-reference). For example I can have a document that has a property that references another object of the document class. This is of course a common feature in other document databases. Is it possible with Weaviate? If yes, are there any concerns I should have with implementing it? e.g.
{
"class": "Document",
"properties": [
{
"name": "title",
"dataType": ["string"]
},
{
"name": "parentDocument",
"dataType": ["Document"]
}
]
}
Thank you!
Hi @gsVAM !! Welcome to our community!!
That’s a really good question, which I don’t have the answer. Yet.
I’ll ping our core team about it.
Thanks for asking!
Hi @gsVAM ! Here it is:
class_obj = {
"class": "Document",
"properties": [
{
"name": "title",
"dataType": ["string"]
},
{
"name": "parentDocument",
"dataType": ["Document"]
}
]
}
# lets create our class
client.schema.create_class(class_obj)
# lets predefine our uuids
d1_uuid = weaviate.util.generate_uuid5("document1")
d2_uuid = weaviate.util.generate_uuid5("document2")
print("document1", d1_uuid)
print("document2", d2_uuid)
# create objects
document1 = client.data_object.create({"title":"Document 1"}, "Document", d1_uuid)
document2 = client.data_object.create({"title":"Document 2"}, "Document", d2_uuid)
# add references
client.data_object.reference.add(
from_class_name="Document",
from_uuid=d1_uuid,
from_property_name="parentDocument",
to_class_name="Document",
to_uuid=d2_uuid
)
now to query this self referential property:
client.query.get("Document", ["title", "parentDocument{...on Document{title}}"]).do()
the resulting Graphql:
{
Get {
Document {
title
parentDocument {
... on Document {
title
}
}
_additional {
id
}
}
}
}
and finally the json response:
{'data': {'Get': {'Document': [{'_additional': {'id': '41fdaad0-e1b2-5d0e-a486-b9b519029264'},
'parentDocument': None,
'title': 'Document 2'},
{'_additional': {'id': 'd895dd7c-deb6-5d5f-8129-379990b2f866'},
'parentDocument': [{'title': 'Document 2'}],
'title': 'Document 1'}]}}}
This is the documentation where it states how to add those references