A record ID names one record: a table name and an ID within that table, such as person:tobie. The Python SDK has two classes for a record ID. RecordID holds the two parts as Python values, and StringRecordId holds a record ID as SurrealQL text for the server to parse. This page documents both classes and the type aliases around them.
from surrealdb import RecordID, RecordIdValue, StringRecordIdSource: record_id.py
Constructor
RecordID(table_name, identifier)Parameter | Type | Description |
|---|---|---|
table_name | str | The name of the table that holds the record. |
identifier | RecordIdValue | The ID of the record within the table: a str, int, uuid.UUID, list, tuple, dictor Range. A Rangetargets every record in the range. |
Examples
RecordID("person", "tobie")RecordID("post", 42)RecordID("reading", ["London", 2025])RecordID("reading", {"city": "Paris", "day": 1})import uuid
RecordID("account", uuid.UUID("019d764a-c692-7631-a182-de9d745eb127"))The type of the ID is part of the record ID. RecordID("post", 42) and RecordID("post", "42") name two different records, so a lookup with the wrong type finds nothing. A tuple is sent as an array and reads back as a list.
The constructor checks the type of both arguments and raises TypeError before anything is sent. A table name must be a str, so RecordID(Table("person"), 1) raises as well. An ID cannot be a bool, float, None, bytes, set or SurrealSet. To have the server generate an ID, pass the table instead, as in db.create(Table("person"), data).
RecordID("person", 1.5)TypeError: RecordID() id must be one of str, int, uuid.UUID, list, tuple, dict or Range, got float: 1.5. SurrealDB has no float record id - use an int, or '1.5' for a string id.Properties
| Property | Type | Description |
|---|---|---|
table_name | str | The table name, without escaping. |
id | Value | The ID within the table, without escaping. |
record = RecordID("person", "tobie")
print(record.table_name, record.id)person tobieid is typed as Value rather than RecordIdValue, because a record ID read from the server can hold any value the server sends. Annotate a value that you build with RecordIdValue.
Record IDs in queries
Pass a RecordID to a query as a variable. It is sent as a typed value, so it needs no escaping and cannot change the meaning of the query text. CRUD methods such as select() and create() take a RecordID directly as their target.
from surrealdb import RecordID, Surreal
with Surreal("ws://localhost:8000") as db:
db.signin({"username": "root", "password": "secret"})
db.use("surrealdb", "docs")
person = RecordID("person", "tobie")
db.create(person, {"name": "Tobie"})
query = db.query("SELECT name FROM $person", {"person": person})
print(query.execute())[[{'name': 'Tobie'}]]A variable can only stand for a whole record ID. SurrealQL does not accept a variable as the ID part of a record ID literal, so person:$id is a parse error. To build a record ID from a variable inside SurrealQL, use type::record("person", $id).
Converting to text
str() renders a record ID as SurrealQL text, for logs and for query text that cannot use a variable. repr() shows the two parts.
print(str(RecordID("person", "tobie")))
print(str(RecordID("users", 123)))
print(str(RecordID("users", "123")))
print(str(RecordID("users", "john-doe")))
print(repr(RecordID("users", "123")))person:tobie
users:123
users:⟨123⟩
users:⟨john-doe⟩
RecordID(table_name=users, record_id='123')A string ID that would otherwise read as a number, or that holds characters such as -, is escaped, and so is a table name that needs it. The SDK escapes with ⟨ ⟩, which every supported server parses. SurrealDB 3.0 and later display the same record ID with backticks, as users:`john-doe` , and read both forms. escape_identifier(name), which is exported from surrealdb, escapes one identifier the same way.
Do not build query text from record.id alone. The string ID "123" and the integer ID 123 give the same characters, so only str(record), which escapes the string, keeps them apart.
RecordID.parse()
RecordID.parse() builds a RecordID from table:id text.
RecordID.parse(record_str)Parameter | Type | Description |
|---|---|---|
record_str | str | Text in the form table:id. |
Returns: RecordID
parse() splits the text at the first :, so an ID can hold colons of its own. The ID is always a string, and the text is not read as SurrealQL. Text without a : raises InvalidRecordIdError.
print(repr(RecordID.parse("person:tobie")))
print(repr(RecordID.parse("user:complex:id")))
print(repr(RecordID.parse("post:42")))RecordID(table_name=person, record_id='tobie')
RecordID(table_name=user, record_id='complex:id')
RecordID(table_name=post, record_id='42')RecordID.parse("post:42") gives the string ID "42", which does not find a record created with the numeric ID 42. For numeric, array or object IDs, call the constructor, or use a StringRecordId so that the server reads the text as SurrealQL.
Equality and hashing
Two RecordID values are equal when their table names are equal and their IDs are equal, including the type of the ID. A RecordID is hashable, so it can be a dict key or a member of a set, and an object ID hashes the same whatever the order of its keys.
print(RecordID("person", "tobie") == RecordID("person", "tobie"))
print(RecordID("post", 42) == RecordID("post", "42"))
owners = {RecordID("person", "tobie"): "admin"}
print(owners[RecordID("person", "tobie")])True
False
adminA RecordID is never equal to a StringRecordId, even when both name the same record.
StringRecordId
A StringRecordId holds a record ID written as SurrealQL text, such as "person:tobie", and the server parses the text. Use it when a record ID arrives as text, for example from a URL or a log, and you want it to mean exactly what it means in SurrealQL. It works wherever a RecordID does: as the target of a CRUD method, as a query variable, and as an end of relate(). A record ID read back from the server is always a RecordID.
A plain str does not work here, because the SDK always reads a plain string target as a table name. db.select("person:tobie") selects from a table that is named person:tobie.
StringRecordId(value)Parameter | Type | Description |
|---|---|---|
value | str | One record ID as SurrealQL text, such as "person:tobie"or "post:42". |
The constructor raises TypeError for a value that is not a str, and InvalidRecordIdError for text without a : or for a range such as "person:1..3". To target a range of records, use RecordID("person", Range(...)).
| Property or method | Type | Description |
|---|---|---|
value | str | The text, as given. |
str(record) | str | The same text. |
from surrealdb import RecordID, StringRecordId, Surreal
with Surreal("ws://localhost:8000") as db:
db.signin({"username": "root", "password": "secret"})
db.use("surrealdb", "docs")
db.create(RecordID("post", 42), {"title": "Hello"})
post = StringRecordId("post:42")
print(db.select(post).execute())
print(repr(db.query("RETURN $post", {"post": post}).first())){'id': RecordID(table_name=post, record_id=42), 'title': 'Hello'}
RecordID(table_name=post, record_id=42)The server reads post:42 as the numeric ID 42, where RecordID.parse("post:42") gives a string ID. The text must be a record ID as SurrealQL writes it, so a string ID that holds - needs backticks: StringRecordId("users:`john-doe`"). SurrealDB 2.x and 4.0 refuse text that they cannot parse, but SurrealDB 3.3 reads as much as it can: it reads StringRecordId("users:john-doe") as users:john. For an ID that comes from user input, build a RecordID from its parts instead.
Pydantic support
RecordID and StringRecordId can be field types in a pydantic model when pydantic is installed, for example with pip install "surrealdb[pydantic]". A field accepts an instance or table:id text. Text for a RecordID field goes through RecordID.parse(), so its ID is a string. In JSON mode both types dump as text, and their JSON schema is a string.
from pydantic import BaseModel
from surrealdb import RecordID
class Person(BaseModel):
id: RecordID
name: str
person = Person(id="person:tobie", name="Tobie")
print(person.id == RecordID("person", "tobie"))
print(person.model_dump(mode="json"))True
{'id': 'person:tobie', 'name': 'Tobie'}Text without a : raises InvalidRecordIdError. To read records straight into such a model, pass into=Person to a CRUD method. See Value types for the other SDK types in a pydantic model.
RecordIdValue
RecordIdValue is the union of the ID types that SurrealDB accepts. It is exported for annotating or casting values that you build.
RecordIdValue = (
str | int | uuid.UUID
| list[Any] | tuple[Any, ...] | dict[str, Any]
| Range
) RecordIdType
RecordIdType is the union that CRUD methods accept as a target. It is not exported from surrealdb; import it from surrealdb.data.types.record_id to annotate your own functions.
RecordIdType = str | RecordID | StringRecordId | TableA str is always a table name, the same as a Table. Pass a RecordID or a StringRecordId to target one record.
See also
Python values for every value class
Table for table references
Range for range targets
Data manipulation for CRUD methods that take a record ID
Record IDs in SurrealQL