Search code examples
vaticle-typedbvaticle-typeql

How to update a random instance of a query in TypeDB?


I have the following error:

Error> [TQL03] TypeQL Error: There is a syntax error at line 11:

delete $connectsTo1 isa connectsTo;
^ extraneous input 'delete' expecting {, 'match', 'define', 'undefine', 'insert'}

I have the following query:

match
$sensor isa Sensor, has id $sensorID;
$segment1 isa Segment, has id $segment1ID;
$segment3 isa Segment, has id $segment3ID;
not { $segment1 is $segment3; };
$monitoredBy1(TrackElement: $segment1, Sensor: $sensor) isa monitoredBy;
$monitoredBy2(TrackElement: $segment3, Sensor: $sensor) isa monitoredBy;
$connectsTo1(TrackElement: $segment1, TrackElement: $segment3) isa connectsTo;
get $segment1ID, $segment3ID;
limit 1;
delete $connectsTo1 isa connectsTo;
insert $segment2 isa Segment, has id 555555;
insert $connectsTo2(TrackElement: $segment1, TrackElement: $segment2) isa connectsTo;
insert $connectsTo3(TrackElement: $segment2, TrackElement: $segment3) isa connectsTo;
insert $monitoredBy3(TrackElement: $segment2, Sensor: $sensor) isa monitoredBy;

This query should find a random instance of two segments that are monitored by the same sensor and they are connected to each other. Then delete the connection between them, create a new segment which wedges in between them and monitored by the same sensor.

I tried to run the query without the delete line. After that the first insert line was succesfull, but with the second insert line i got this error: ## Error>[THW15] Invalid Thing Write: The thing variable '$segment1' cannot be inserted as a new instance without providing its type (isa). It seems like the query doesn't recognises $segment1 anymore.


Solution

  • The get clause makes it the match query. TypeDB Studio just executes these lines as multiple queries. And delete query can't be without a match clause.

    If you delete the delete clause it will still see multiple queries - match query (with match and get clauses) and then insert query (which can be without a match clause, but it's lacking the variable bounding in this particular case).

    I would suggest deleting the get clause. Then it should see the update (or match-delete-insert) query. Alternatively, you can try splitting these into multiple queries with match clauses for every query.

    P.S. TypeDB language-specific libraries won't have this problem as in them you manually send each query and even select its type (every query type has a separate method to send the query).

    P.P.S. Oh, and those insert keywords at the end - they are also creating separate queries (at least the TypeDB Studio treats them like that). If your intent was to send all these as a single query - drop the duplicate inserts. Only the first one should be sufficient.