How to add a new property to an existing object

Is there any way to update an object by adding new property to it. For example, I have an object with the properties

{
'ref_id':'id_01',
'name':'xxx',
}

Can I add a new property named ‘phone_no’:‘123345’ to this particular object where the property ‘ref_id’ is unique for each object.

Hi @Jegadeesh,

Weavatie supports updating (and adding properties) objects based on uuid.
However, it doesn’t support selecting updated objects based on a filter (which is what we would need to make this work).

Here is how you can add a property – if you have an uuid of the object. (Link to docs)

jeopardy = client.collections.get("JeopardyQuestion")
jeopardy.data.update(
    uuid=uuid_to_update,
    properties={
        "phone_no": "123345",
    }
)

(Recommended Solution) – generate UUID based on ref_id

You can Generate deterministic IDs, based on a unique property (Link to docs).

In your case this is your ref_id. Like this:

from weaviate.util import generate_uuid5
new_uuid = generate_uuid5("id_01")

Then when you insert data into Weaviate, make sure to generate a UUID for each object. Like this:

import weaviate.classes.data as wd
from weaviate.util import generate_uuid5  # Generate a deterministic ID

col = client.collections.get("MyCol")

# Insert data objects
response = col.data.insert_many([
    wd.DataObject(
        properties={
            "ref_id":"id_01",
            "name":"xxx",
        },
        uuid=generate_uuid5("id_01")
    ),
    wd.DataObject(
        properties={
            "ref_id":"id_02",
            "name":"yyy",
        },
        uuid=generate_uuid5("id_02")
    ),
    wd.DataObject(
        properties={
            "ref_id":"id_03",
            "name":"zzz",
        },
        uuid=generate_uuid5("id_03")
    ),
])

Then you could easily retrieve any object by ref_id. Like this:

item = col.query.fetch_object_by_id(generate_uuid5("id_03"))
print(item.properties)

And most importantly, update using your ref_id, like this:

col = client.collections.get("MyCol")

col.data.update(
    uuid=generate_uuid5("id_03"),
    properties={
        "phone_no": "123345"
    }
)

Let me know if this works for you.