In SurrealDB, nodes are typically records in ordinary tables (users, posts, companies, and so on). Graph edges are also real tables, created and queried with RELATE.
Creating nodes and edge records
SurrealDB uses RELATE to form the usual graph triple: subject → predicate → object.
Using RELATE, you can model primary relationships from something like an e-commerce flow: wish list, cart, order, and review. These become your edge tables.
Here, existing record IDs from the person and product tables are used. The edge tables sit between them and are created by these RELATE statements.
After you run RELATE, edge records gain two standard fields: in and out. The in field holds the ID of the record on the left side of the statement, and out the ID on the right. The triple can also be read as:
in -> id -> out
where the first node is in, the edge is the record’s id, and the second node is out.
Adding data to edge tables with SET and CONTENT
What sets SurrealDB apart from graph-only databases is that edges are tables, so you can store fields on them like any other row.
You can both create an order relationship and query through it to pull data from product and person:
That shows how to read fields on the edge table directly. For traversing with arrow syntax from nodes, see Graph traversal.
Creating a graph relation with metadata on the edge
This pattern is like the examples above, except the user row does not keep a comments field. A wrote edge connects user and comment, and the edge row stores context (location, device, mood):
Once the edge exists, you can traverse it with the arrow operator (forward, backward, recursively, and more. See Graph traversal for patterns such as:
Define a table as a relation for type safety and Surrealist
Defining a table as TYPE RELATION restricts it to valid graph edges between records.
Adding TYPE RELATION to DEFINE TABLE is enough to enforce that behaviour.
Constraining in and out record types ensures only intended record types can be linked:
Strict relation definitions also feed Surrealist’s Designer view.
Create some data and relate it:
Before the table is defined as a relation, Surrealist only sees schemaless tables:

Defining the table as a TYPE RELATION clarifies that likes is a graph table:

With IN and OUT specified, Designer can show the full relation:

Unique index for relations “between equals”
When an edge represents friendship, partnership, sister cities, and similar symmetric roles, it may not matter which side is in or out), but you still want at most one edge for the pair.
Define a field from the sorted in and out values and put a unique index on it:
Then a second RELATE from the other direction hits the index:
Querying symmetric edges with <-> is covered in Graph traversal. For friendship-oriented examples, see Social network patterns.
RELATE before both endpoints exist
Graph edge rows can exist before the two endpoint records are created:
To forbid that, add ENFORCED on the relation table:
Some workflows rely on creating the edge first (for example, a street with predictable house IDs before houses exist. A DEFINE FIELD ... VALUE can describe the path from house to street and resolve once the house exists: