< How to do upsert in typeql?>

< How to do upsert in typeql?>
2 Replies
kenho811
kenho811OP4w ago
Say I have a entity called company with definition
define entity company owns company-id @key,
owns name,
owns founded-on;
attribute company-id, value string;
attribute name, value string:
attribute founded-on, value date;

define entity company owns company-id @key,
owns name,
owns founded-on;
attribute company-id, value string;
attribute name, value string:
attribute founded-on, value date;

Now when I insert data into this graph, I want to do upsert. Reasons is, I might have multiple data sources for the same company-id, and yet each source only contributes to one of the many fields owned by company. Say source A has company-id: 1, name: "ABC Limited" And source B has company-id:1, founded-on "2025-01-01" Then I feel the easiest way is to write upsert, based on company-id as key. ============= I read about put keyword. However, the doc says that put is basically - match all the patterns of a given thing. If all fields match, then do nothing. Otherwise, if partially matched or not matched at all, insert. So, it looks like that I cannot specify that "just look at the company-id field when doing matching`. So what would be the correct way to do upsert? Or if upsert is not possible, what's the recommended approach to solve this?
krishnan
krishnan4w ago
The reference gives an example: "To achieve the desired result of not re-creating the person if only the age attribute is missing, put stages can be pipelined." I assume this is what you want - attach the founded-on to the company with company-id == 1, if it exists (but create the company if it does not exist)
put $c isa company, has company-id 1;
put $c has founded-on 2025-01-01;
put $c isa company, has company-id 1;
put $c has founded-on 2025-01-01;

Did you find this page helpful?