RecordID
The RecordID struct represents a SurrealDB record identifier. A record ID consists of a table name and an identifier value, providing a typed way to reference records without ambiguity from string parsing.
Package: github.com/surrealdb/surrealdb.go/pkg/models
Source: pkg/models/record_id.go
Definition
type RecordID struct {
Table string
ID any
}
The ID field can be any CBOR-serializable value: a string, integer, array, or map.
Constructors
NewRecordID
Creates a new RecordID with the given table name and ID.
Syntax
id := models.NewRecordID(tableName, id)
| Parameter | Type | Description |
|---|
tableName required | string | The table name. |
id required | any | The record identifier value. |
Returns: RecordID
Examples
id := models.NewRecordID("persons", "tobie")
numericID := models.NewRecordID("events", 42)
compositeID := models.NewRecordID("access", []any{"us", 2026})
ParseRecordID
Parses a string of the form "table:id" into a RecordID. Only works for simple IDs without colons.
Syntax
id, err := models.ParseRecordID(idStr)
| Parameter | Type | Description |
|---|
idStr required | string | A string in the form “table:id”. |
Returns: (*RecordID, error)
Returns ErrBadRecordID if the string does not contain exactly one colon.
Methods
.String()
Returns the string representation of the record ID in the form "table:id".
Syntax
s := id.String()
Returns: string
.SurrealString()
Returns the SurrealQL representation of the record ID in the form r'table:id'.
Syntax
s := id.SurrealString()
Returns: string
CBOR Encoding
RecordID is encoded as CBOR tag 8 containing a two-element array [table, id]. The SDK handles marshaling and unmarshaling automatically when you use RecordID in structs.
type Person struct {
ID *models.RecordID `json:"id,omitempty"`
Name string `json:"name"`
}
See Also