NEW

The wait is over. SurrealDB 3.0 is here!

Learn more
Course content preview

19: Basic graph relations

The record link method we used a few pages ago is the most straightforward way to make a link. Here is how we created a record link back on page 12:

UPDATE town:riverdale SET libraries = [place:surreal_library];

{/ Since this course needs to be simple, don't add record references until they are stable /}

While straightforward to use, record links are simple connections between records that don't contain any information about the link itself.

But sometimes we might also want information about the link - the relation - between two records. If a library adds a new customer, it would be nice to have a field that shows when the person became a customer, such as SET customer_since = d'2025-02-11'.

And a field like customer_since doesn't belong to person or a place, but to the relation between the two.

To accomplish this, we can use a second type of link called a graph edge. These edges are created and queried using their own syntax that uses arrows.

This arrow syntax is easiest to get used to by thinking about the links and queries we want to make. You can query in one direction using -> arrows to the right:

I want to know...PathPath explanationPossible metadata
...where people workperson->works_at->placeperson to works_at to placeWhen did the person start? How much is the salary?
...which books people likeperson->likes->bookperson to likes to bookHow much does the person like a book?

Or the other direction using <- arrows to the left.

I want to know...PathPath explanationPossible metadata
...who works at a placeplace<-works_at<-personplace to being "worked at" by a personSame as above
...who likes a certain bookbook<-likes<-personbook to being liked by a personSame as above

As the paths show, we aren't dealing with direct links this time, but a table in the middle that has a certain name (like works_at or likes). The table in the middle is a graph edge, which always contains two fields called in and out. The in field is where the ID goes for the record that does something, and the out field for the record goes that has something done to it.

And the table in the middle can contain other fields, just like regular records do.

Now that we have the basics down, we'll start making some graph relations in the next page.