SurrealDB Docs Logo

Enter a search query

.InsertRelation[T](table, data)

Inserts one or multiple relations in the database.

Method Syntax
db.InsertRelation[T](table, data)

Arguments

ArgumentsDescription
table required

Optionally pass along a table to insert into.

data required

Either a single document/record or an array of documents/records to insert

Example usage

type Likes struct { ID models.RecordID `json:"id"` In models.RecordID `json:"in"` Out models.RecordID `json:"out"` } // Insert a single record into the "likes" relation like, err := surrealdb.Insert[Likes](db, models.Table("likes"), map[interface{}]interface{}{ "in": models.NewRecordID("person", "tobie"), "out": models.NewRecordID("post", "123"), }) if err != nil { panic(err) } fmt.Printf("Inserted single like relation: %+v\n", like) // Insert multiple records into the "likes" relation likes, err := surrealdb.Insert[[]Likes](db, models.Table("likes"), []map[interface{}]interface{}{ { "in": models.NewRecordID("person", "tobie"), "out": models.NewRecordID("post", "123"), }, { "in": models.NewRecordID("person", "jaime"), "out": models.NewRecordID("post", "456"), }, }) if err != nil { panic(err) } fmt.Printf("Inserted multiple like relations: %+v\n", likes)

On this page

© SurrealDB GitHub Discord Community Cloud Features Releases Install