SurrealDB.store, the seller will always be SurrealDB. Which is why we’ve created this one-to-many relationship.
An important thing to notice here is that we haven’t created the seller table yet, but we are still able to insert the seller
record ID into the product table because there are no foreign key constraints for record links.
CREATE seller:surrealdb SET name = "SurrealDB", email = "education@surrealdb.com"
Let’s therefore quickly create the seller
table such that our product record link will actually go somewhere.
We can now make bidirectional relationships to and from address_history
and payment_details
to the person
.
CREATE address_history:ulid(), payment_details:ulid() SET person = person:01GFFXDCG89SAR3WM2SDV2E1RA PARALLEL;
Starting with inserting a person
record ID into both tables. We can now query in one direction, from address_history
and payment_details
to person
.
UPDATE person:01GFFXDCG89SAR3WM2SDV2E1RA MERGE { address_history: address_history:01HCWCEB1R8Y499XJYPVWE04RX, payment_details: payment_details:01FTZ3MR7095CRDT1A04NNRQ6H };
To make this bidirectional, we need to insert the address_history
and payment_details
record IDs into the person
table as well.
An important thing to note is that this will only create a link between these individual records, not every record in each table because we have specified the IDs
UPDATE product SET seller = seller:surrealdb;
To link every record together, we’d need to not specify any record ID in the person
table, this will add the link to all records in the table. Like we did in the previous example
We’ve already covered how to create record links, let’s now explore how to use them in the rest of our CRUD operations.
-- Selecting the current address from the person table SELECT person.address.address_line_1 AS current_address, addresses[0].address_line_1 AS previous_address FROM address_history:01HCWCEB1R8Y499XJYPVWE04RX; -- Adding the current address to the address_history table UPDATE address_history:01HCWCEB1R8Y499XJYPVWE04RX SET addresses += person.address; -- Deleting the address history from Leoma DELETE address_history WHERE person.name = "Leoma Santiago"
As these examples show, you can use record links in CRUD operations in the same way as you would, if it was all embedded in the same record, using the dot and bracket notation.
Now that we’ve interlinked our knowledge of record links, let’s summarise what we’ve learned.
Record links:
address_history
and payment_details
tables.You can use record links in CRUD operations in the same way as you would, if it was all embedded in the same record, using the dot and bracket notation.
That’s everything about record links, I’ll see you in the next lesson, where we’ll explore relational style joins.