typedb / typeql

TypeQL: the modern query language of TypeDB
https://typedb.com
Mozilla Public License 2.0
218 stars 45 forks source link

Insert independently of match and delete #254

Open nikolaimerritt opened 1 year ago

nikolaimerritt commented 1 year ago

Description

Calling match and delete with one variable, and insert with another, results in a TypeQL Error. The insert should be independent of the match and delete.

Environment

  1. OS (where TypeDB server runs): Mac OS 12.6
  2. TypeDB version (and platform): TypeDB Cluster 2.13
  3. TypeDB client: TypeDB Studio 12.13

Reproducible Steps

Steps to create the smallest reproducible scenario:

  1. Create the minimal schema
    
    define 

name sub attribute, value string;

person sub entity, owns name @key;

2. Run the minimal query

match $a isa person, has name "alice"; delete $a isa person; insert $b isa person, has name "bob";


## Expected Output
`alice` to be deleted, if exists. `bob` to be created.

## Actual Output
The TypeQL error is generated

[TQL18] TypeQL Error: None of the variables in 'insert' ('[$b isa person, has name "bob"]') is within scope of 'match' ('[$a]')

flyingsilverfin commented 1 year ago

Can I clarify whether you intended the insert to be a separate query from the match-delete or a part of a match-delete-insert query flow?

Thanks!

bradenmacdonald commented 5 months ago

I'm encountering the same issue... I'm trying to write a big insert script like this example to populate a database, but I need to be able to run it over and over to iterate on it. So I want it to start with a "delete all data from the database" like those examples do (unless there's an upsert statement, which I couldn't see).

However, if I put match $all isa thing; delete $all isa thing; at the beginning of the .tql file, before a bunch of unrelated insert statements, it gives that error: None of the variables in 'insert' ... is within scope of 'match' ('[$all]').

It seems like you have to use a hack to work around this, by putting a single insert statement immediately following the match $all isa thing; delete $all isa thing;, such as insert $all isa ______, has....; and then you can follow it with as many unrelated inserts as you want. This is an awkward constraint though; I would rather just be able to run the "delete all" and then specify that the following inserts are unrelated and don't need to be tied to the same variables.