Search code examples
javascriptsurrealdb

Update records stored in array in other record


I have the following record-structures shown in JSON/Typescript format:

(tablename = dimension)
interface Dimension {
    name: string,
    units: Unit[]
}

(tablename = unit)
interface Unit {
    symbol: string,
    is_base: boolean,
}

I want to change the property is_base to false on all units within a dimension in one query. How do i do this? I am using surrealdb.wasm in a Javascript project (I believe this uses Indxdb).

I have tried the following query:

UPDATE dimension:myDimensionID.units.* SET is_base = false

And the query seems to execute successfully, but it does not change the property is_base on any unit record.


Solution

  • Record links don't currently work on update statements, so you can't change the value of a linked table.

    If embedding units you can do this

    create dimension:test content {
    name: "stuff",
    units : [{is_base: true}, {is_base: true}]
    };
    
    UPDATE dimension:test SET units.is_base = false;
    

    If units are a separate table, you can do this if you have a field of which dimension it belongs to.

    update unit 
    SET is_base = false
    WHERE dimension = "dimension:test"