Can't update objects with an optional relations on...
# community-help
o
Can't update objects with an optional relations on member field I'm using optional relations in some of my collections without issues, but I think I'm hitting a bug on update of such fields. I've not been able if this also applies to references define at root level of collections or only on sub objects. Here is how my relation is defined in the problematic collection:
Copy code
{
      "name": "relatedassetsdata",
      "type": "object[]",
      "sort": false,
      "facet": false,
      "index": true,
      "optional": true
    },
    {
      "name": "relatedassetsdata.childobjecttid",
      "type": "string[]",
      "sort": false,
      "facet": false,
      "index": true,
      "reference": "objects.id",
      "optional": true
    },
Writing to that collection when it is empty works with relatedassetsdata.childobjecttid being set or null, however when I try to update that same document with null relatedassetsdata.childobjecttid I'm getting:
Copy code
Field `relatedassetsdata.childobjecttid` must have string value.
If I delete the document and resend the exact same payload, it works. But following updates will fail. I want to add that my update do not change relatedassetsdata.childobjecttid: it is null at creation and remains null in my update. Any idea what could be wrong? Should I issue a bug report?
f
Hey @Olivier , Could you provide small reproducible example like this one so we can debug this issue locally?
o
Here we go @Fanis Tharropoulos Inserted below as I can't upload a .sh file apparently ;)
Copy code
#!/bin/bash
### Typesense Bug Reproduction: Optional Reference in Nested Object Array ########
# Bug: Can't update documents with optional relations on nested object member fields
# When updating a document where a nested optional reference field is null,
# Typesense throws: "Field `fieldname` must have string value"
# Even though the field is marked as optional and was null on creation
set -x

export TYPESENSE_API_KEY=xyz
export TYPESENSE_HOST=http://localhost:8108

# Clean slate
docker stop typesense-repro 2>/dev/null
docker rm typesense-repro 2>/dev/null
rm -rf "$(pwd)"/typesense-data-dir-repro
mkdir "$(pwd)"/typesense-data-dir-repro

# Start Typesense
docker run -d -p 8108:8108 --name typesense-repro \
            -v"$(pwd)"/typesense-data-dir-repro:/data \
            typesense/typesense:30.1 \
            --data-dir /data \
            --api-key=$TYPESENSE_API_KEY \
            --enable-cors

# Wait for Typesense to be ready
until curl -s -o /dev/null -w "%{http_code}" "$TYPESENSE_HOST/health" -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | grep -q "200"; do
  echo "Waiting for Typesense..."
  sleep 2
done

echo "=== Typesense Ready ==="
curl -s "$TYPESENSE_HOST/debug" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | jq

# Step 1: Create referenced collection (represents the target of the reference)
echo ""
echo "=== Step 1: Creating 'objects' collection (referenced collection) ==="
curl -s "$TYPESENSE_HOST/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
             "name": "objects",
             "fields": [
               {"name": "title", "type": "string" },
               {"name": "code", "type": "string" }
             ]
           }' | jq

# Add a sample object document
echo ""
echo "=== Adding sample document to 'objects' collection ==="
curl -s "$TYPESENSE_HOST/collections/objects/documents" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{"id": "obj-123", "title": "Sample Object", "code": "OBJ001"}' | jq

# Step 2: Create main collection with nested object array containing optional reference
echo ""
echo "=== Step 2: Creating 'items' collection with nested optional reference ==="
curl -s "$TYPESENSE_HOST/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
             "name": "items",
             "enable_nested_fields": true,
             "fields": [
               {"name": "recordidentifier", "type": "string" },
               {"name": "title", "type": "string" },
               {"name": "relatedassetsdata", "type": "object[]", "optional": true },
               {"name": "relatedassetsdata.id", "type": "int32[]", "optional": true },
               {"name": "relatedassetsdata.relationtype", "type": "string[]", "optional": true },
               {"name": "relatedassetsdata.childobjectid", "type": "string[]", "reference": "objects.id", "optional": true }
             ]
           }' | jq

# Step 3: Create initial document with null optional reference field
echo ""
echo "=== Step 3: Creating document with NULL optional reference field (SHOULD WORK) ==="
curl -s "$TYPESENSE_HOST/collections/items/documents" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
              "id": "item-001",
              "recordidentifier": "REC001",
              "title": "Test Item with Null Reference",
              "relatedassetsdata": [
                {
                  "id": 1,
                  "relationtype": "Related",
                  "childobjectid": null
                }
              ]
            }' | jq

# Verify document was created
echo ""
echo "=== Verifying document was created successfully ==="
curl -s "$TYPESENSE_HOST/collections/items/documents/item-001" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | jq

# Step 4: Update the document (keeping the reference field as null)
echo ""
echo "=== Step 4: Updating document with NULL optional reference UNCHANGED (BUG: SHOULD WORK BUT FAILS) ==="
echo "Expected: Success (field is optional and remains null)"
echo "Actual: Error - 'Field must have string value'"
echo ""
curl -s "$TYPESENSE_HOST/collections/items/documents/item-001" \
        -X PATCH \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
              "title": "Updated Test Item - Reference Still Null",
              "relatedassetsdata": [
                {
                  "id": 1,
                  "relationtype": "Related",
                  "childobjectid": null
                }
              ]
            }' | jq

# Step 5: Show that DELETE and re-INSERT works
echo ""
echo "=== Step 5: Deleting document ==="
curl -s "$TYPESENSE_HOST/collections/items/documents/item-001" \
       -X DELETE \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | jq

echo ""
echo "=== Step 6: Re-inserting EXACT SAME document (WORKS) ==="
curl -s "$TYPESENSE_HOST/collections/items/documents" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
              "id": "item-001",
              "recordidentifier": "REC001",
              "title": "Updated Test Item - Reference Still Null",
              "relatedassetsdata": [
                {
                  "id": 1,
                  "relationtype": "Related",
                  "childobjectid": null
                }
              ]
            }' | jq

# Step 7: Demonstrate that updating WITH a valid reference works
echo ""
echo "=== Step 7: Creating document WITH valid reference ==="
curl -s "$TYPESENSE_HOST/collections/items/documents" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
              "id": "item-002",
              "recordidentifier": "REC002",
              "title": "Test Item with Valid Reference",
              "relatedassetsdata": [
                {
                  "id": 2,
                  "relationtype": "Related",
                  "childobjectid": "obj-123"
                }
              ]
            }' | jq

echo ""
echo "=== Step 8: Updating document that HAS a valid reference (SHOULD WORK) ==="
curl -s "$TYPESENSE_HOST/collections/items/documents/item-002" \
        -X PATCH \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
              "title": "Updated - Still has valid reference",
              "relatedassetsdata": [
                {
                  "id": 2,
                  "relationtype": "Related",
                  "childobjectid": "obj-123"
                }
              ]
            }' | jq

# Cleanup
echo ""
echo "=== Cleanup ==="
docker stop typesense-repro
docker rm typesense-repro

### Documentation ######################################################################################
# This script demonstrates a bug where optional reference fields in nested object arrays
# cannot be updated when they are null, even though they are marked as optional.
# The error only occurs on UPDATE operations, not on initial CREATE operations.
Hi @Fanis Tharropoulos, Just following up on this. Is it indeed a bug?
f
Hey Olivier, Yes, we're actually checking in order to merge the fix for this. Thank you for bringing this into light
🙏 1
o
Hi again @Fanis Tharropoulos, Any idea when this would get released? This is hitting me pretty hard ;)
f
Hey Olivier, We don't have a timeline for this PR currently. We'll reply in this thread when we have it merged.
🙏 1
o
Hi, Just wanted to add that this also applies without relations defined. I've been hitting the same error updating that string array without a reference defined. This seems to only happen when I define the sub-field in my collection, not if "autoschema" (ie: enable_nested_fields: true,) handles it.
Sorry @Fanis Tharropoulos, correction: This is also not working with autoschema, because once I've imported some items, relatedassetsdata.id will be auto-populated in the collection definition, and updates prevented. This is really problematic: you basically can't update any string array within an object. Please let me know when a bugfix may happen, thanks.
f
Hey Olivier, Could you provide a small reproducible example of your sub-issue so I can see if our fix handles it?
o
@Fanis Tharropoulos, I have some troubles reproducing right now. I thought this was fixed a few times put it continues to pop up in our sequin replication. I'll provide an updated script ASAP.
f
Hey Olivier, Thank you for that. Isolating it into a distinct reproducible example will help us find if this is related to just Typesense, or maybe the Sequin integration.
👍 1
o
@Fanis Tharropoulos, I confirm this was an error on my side related to a wrong pipeline script. So only the initial part of the bug is valid (ie: with reference in subobjects).
a
Hey Olivier, We've released a fix for the bug you mentioned. Could you try upgrading to v31.0.rc7 forward and check?