Skip to content

Dynamic relationship creation #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions pages/data-migration/csv.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ There are also two variations of the files: files with a header and files withou
- [`people_relationships_wh.csv`](https://public-assets.memgraph.com/import-data/load-csv-cypher/one-type-nodes/people_relationships_wh.csv)<br/>
The file contains the following data:
```plaintext
id_from,id_to
100,101
100,102
100,103
101,103
102,104
id_from,id_to,type
100,101,IS_FRIENDS_WITH
100,102,IS_FRIENDS_WITH
100,103,IS_FRIENDS_WITH
101,103,IS_FRIENDS_WITH
102,104,IS_FRIENDS_WITH
```
</Tabs.Tab>
<Tabs.Tab>
Expand Down Expand Up @@ -285,13 +285,7 @@ There are also two variations of the files: files with a header and files withou
```

If successful, you should receive an `Empty set (0.014 sec)` message.

You can also create, set, or remove labels using property values, here is an example:

```cypher
LOAD CSV FROM "/path-to/people_nodes_wh.csv" WITH HEADER AS row
CREATE (p:row.label {id: row.id, name: row.name});
```
Notice how **node labels can be dynamically created* from the CSV file.

</Tabs.Tab>
<Tabs.Tab>
Expand Down Expand Up @@ -335,10 +329,11 @@ There are also two variations of the files: files with a header and files withou
```cypher
LOAD CSV FROM "/path-to/people_relationships_wh.csv" WITH HEADER AS row
MATCH (p1:Person {id: row.id_from}), (p2:Person {id: row.id_to})
CREATE (p1)-[:IS_FRIENDS_WITH]->(p2);
CREATE (p1)-[:row.type]->(p2);
```

If successful, you should receive an `Empty set (0.014 sec)` message.
Notice how **relationship types can be dynamically created** from the CSV file.

</Tabs.Tab>
<Tabs.Tab>
Expand Down
46 changes: 46 additions & 0 deletions pages/querying/clauses/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ more details.
1.1. [Creating a single node](#11-creating-a-single-node)<br />
1.2. [Creating a node with properties](#12-creating-a-node-with-properties)<br />
1.3. [Creating multiple nodes](#13-creating-multiple-nodes)<br />
1.4. [Creating node labels dynamically](#14-creating-node-labels-dynamically)<br />
2. [Creating relationships](#2-creating-relationships)<br />
2.1. [Creating a relationship between two nodes](#21-creating-a-relationship-between-two-nodes)<br />
2.2. [Creating a relationship with properties](#22-creating-a-relationship-with-properties)<br />
2.3. [Creating relationship types dynamically](#15-creating-relationship-types-dynamically)<br />
3. [Creating a path](#3-creating-a-path)
4. [Creating an enum](#4-creating-an-enum)

Expand Down Expand Up @@ -116,6 +118,28 @@ Output:
+------------+------------+
```

### 1.4. Creating node labels dynamically

Node labels can be created dynamically from variable values. The functionality only works with CREATE.
Matching and merging of dynamic node labels is not supported since query plan and scanning indices are created upfront.

```cypher
WITH {label_value: "Label"} as x
CREATE (n:x.label_value) RETURN n;
```

Output:
```nocopy
+------------+
| n |
+------------+
| (:Label) |
+------------+
```

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary labels
into the graph.

## 2. Creating relationships

### 2.1. Creating a relationship between two nodes
Expand Down Expand Up @@ -176,6 +200,28 @@ Output:
+---------------------------------+
```

### 2.3. Creating relationship types dynamically

Relationship types can be created dynamically from variable values. The functionality only works with CREATE.
Matching and merging of dynamic relationship types is not supported since query plan and scanning indices are created upfront.

```cypher
WITH {edge_type_value: "EDGE_TYPE"} as x
CREATE ()-[r:x.edge_type_value]->() RETURN r;
```

Output:
```nocopy
+--------------+
| n |
+--------------+
| [:EDGE_TYPE] |
+--------------+
```

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary
edge types into the graph.

## 3. Creating a path

When creating a path all the entities of the pattern will be created.
Expand Down