-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
--- | ||
sidebar_position: 17 | ||
sidebar_label: Record references | ||
title: Record references | SurrealQL | ||
description: One of the most powerful features of SurrealDB is the ability to traverse from record-to-record without the need for traditional SQL JOINs. Each record ID points directly to a specific record in the database. | ||
|
||
--- | ||
|
||
import Since from '@components/shared/Since.astro' | ||
|
||
# Record references | ||
|
||
<Since v="v2.2.0" /> | ||
|
||
## Basic concepts | ||
|
||
Reference tracking begins by adding a `REFERENCE` clause to any `DEFINE FIELD` statement, as long as the field is a `record` or array of records. | ||
|
||
```surql | ||
DEFINE FIELD comics ON person TYPE option<array<record<comic_book>>> REFERENCE; | ||
``` | ||
|
||
Any referencing record can then be picked up on the referenced record by creating a field of type `references`. | ||
|
||
```surql | ||
DEFINE FIELD owners ON comic_book TYPE references; | ||
``` | ||
|
||
```surql | ||
CREATE person:one, person:two SET comics = [comic_book:one]; | ||
CREATE comic_book:one SET title = "Loki, God of Stories"; | ||
SELECT * FROM comic_book; | ||
``` | ||
|
||
The linking records will now be picked up automatically from the `comic_book` side. | ||
|
||
```surql title="Output" | ||
[ | ||
{ | ||
id: comic_book:one, | ||
owners: [ | ||
person:one, | ||
person:two | ||
], | ||
title: 'Loki, God of Stories' | ||
} | ||
] | ||
``` | ||
|
||
## Specifying linking tables | ||
|
||
The following is similar to the previous example, except that the `comic_book` is now being linked to from two sources, one of which is a `publisher` which publishes both books and comic books. | ||
|
||
```surql | ||
DEFINE FIELD comics ON person TYPE option<array<record<comic_book>>> REFERENCE; | ||
DEFINE FIELD products ON publisher TYPE option<array<record<comic_book|book>>> REFERENCE; | ||
DEFINE FIELD owners ON comic_book TYPE references; | ||
CREATE person:one, person:two SET comics = [comic_book:one]; | ||
CREATE publisher:one SET products = [comic_book:one, book:one]; | ||
CREATE comic_book:one SET title = "Loki, God of Stories"; | ||
SELECT * FROM comic_book; | ||
``` | ||
|
||
Because the `owners` field on `comic_book` looks for any and all references, it now includes the publisher as an owner, which is not ideal. | ||
|
||
```surql title="Output" | ||
[ | ||
{ | ||
id: comic_book:one, | ||
owners: [ | ||
person:one, | ||
person:two, | ||
publisher:one | ||
], | ||
title: 'Loki, God of Stories' | ||
} | ||
] | ||
``` | ||
|
||
This can be fixed by changing the single field of type `references` to two fields, one of which is a `references<person>`, and the other a `references<publisher>`. | ||
|
||
```surql | ||
REMOVE TABLE person; | ||
REMOVE TABLE comic_book; | ||
REMOVE TABLE publisher; | ||
REMOVE TABLE book; | ||
DEFINE FIELD comics ON person TYPE option<array<record<comic_book>>> REFERENCE; | ||
DEFINE FIELD products ON publisher TYPE option<array<record<comic_book|book>>> REFERENCE; | ||
DEFINE FIELD owners ON comic_book TYPE references<person>; | ||
DEFINE FIELD publisher ON comic_book TYPE references<publisher>; | ||
CREATE person:one, person:two SET comics = [comic_book:one]; | ||
CREATE publisher:one SET products = [comic_book:one, book:one]; | ||
CREATE comic_book:one SET title = "Loki, God of Stories"; | ||
SELECT * FROM comic_book; | ||
``` | ||
|
||
```surql title="Output" | ||
[ | ||
{ | ||
id: comic_book:one, | ||
owners: [ | ||
person:one, | ||
person:two | ||
], | ||
publisher: [ | ||
publisher:one | ||
], | ||
title: 'Loki, God of Stories' | ||
} | ||
] | ||
``` | ||
|
||
## Specifying linking tables and/or field names | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters