SurrealDB Docs Logo

Enter a search query

.Relate()

Creates a relation between records.

Method Syntax
db.Relate(table, @in, @out, data)

Arguments

ArgumentsDescription
thing required

The table name or a RecordId to create.

@in required

The edge of the relation.

@out required

The other edge of the relation.

data optional

The document / record data to insert.

cancellationToken optional

The cancellationToken enables graceful cancellation of asynchronous operations.

Example usage

package main import ( "fmt" "time" "github.com/surrealdb/surrealdb.go" "github.com/surrealdb/surrealdb.go/pkg/models" ) func main() { // Assuming db is an instance of your database connection. db, err := surrealdb.New("ws://localhost:8000/rpc") if err != nil { fmt.Println("Error connecting to DB:", err) return } // Create a new relationship. relationship := &surrealdb.Relationship{ In: models.RecordID{Table: "user", ID: "john"}, Out: models.RecordID{Table: "post", ID: "123"}, Relation: "wrote", Data: map[string]interface{}{ "created_at": time.Now(), "pages": 14, }, } // Use the Relate function from the surrealdb package. err = surrealdb.Relate(db, relationship) if err != nil { fmt.Println("Error creating relation:", err) return } fmt.Printf("Created relation with ID: %v\n", relationship.ID) } }