SurrealDB Datetime ID Issue Troubleshooting Record Creation Failure
Introduction
In this article, we delve into a specific issue encountered while using SurrealDB, a cloud-native distributed database. The problem arises when attempting to create records with datetime IDs, leading to a “not a valid id” error. This comprehensive guide will walk you through the bug report, the steps to reproduce the issue, the expected behavior, and the current SurrealDB version affected. We will also explore potential causes and solutions to this problem. SurrealDB is a powerful database, and understanding these nuances is crucial for developers building applications on it.
Bug Report Overview
The bug report highlights a scenario where creating records in SurrealDB using a datetime value as the ID fails. The error message “Found d'2025-07-06T15:31:25.288213531Z' for the Record ID but this is not a valid id” clearly indicates an issue with how datetime values are being handled as record IDs. This is a significant problem as it prevents developers from using timestamps as unique identifiers for their data, which is a common practice in many applications. Let's dive deeper into the steps to reproduce this bug and understand the underlying cause. This issue affects the SurrealDB Server component, making it essential to address for the stability and usability of the database.
Steps to Reproduce
The bug can be reproduced using Surrealist, a command-line tool for interacting with SurrealDB. The following steps outline the process:
-
Define a Table:
First, a table named
test
is defined with schemaful validation. This means that the table will enforce a predefined schema for the records it stores. Permissions are set to allow select, update, and delete operations only for authenticated users ($auth.id != none
). This ensures that only authorized users can modify the data.DEFINE TABLE test SCHEMAFULL PERMISSIONS FOR select, update, delete WHERE $auth.id != none;
-
Define Fields:
Next, the fields for the
test
table are defined. These fields include:id
: A datetime field intended to store the record's ID.raw_data
: A bytes field to store binary data.length
: An integer field to store the length of theraw_data
.check_sum
: A string field to store a checksum for data integrity.
DEFINE FIELD id ON TABLE test TYPE datetime; DEFINE FIELD raw_data ON TABLE test TYPE bytes; DEFINE FIELD length ON TABLE test TYPE int; DEFINE FIELD check_sum ON TABLE test TYPE string;
-
Create a Record:
Finally, an attempt is made to create a record in the
test
table. Theid
field is set to the current timestamp using thetime::now()
function. Thecheck_sum
field is set to an empty string, thelength
field to 0, and theraw_data
field to a byte string.create test content { id:time::now(), check_sum:"", length:0, raw_data:<bytes>"I am some bytes" };
-
Observe the Error:
The execution of the
create
query results in the error message: "Found d'2025-07-06T15:31:25.288213531Z' for the Record ID but this is not a valid id". This error indicates that SurrealDB is not correctly interpreting the datetime value as a valid record ID. This issue is critical as it prevents the use of datetime values as unique identifiers, limiting the flexibility of the database schema.
Expected Behavior
The expected behavior is that the record should be created successfully with the datetime value generated by time::now()
as the ID. This aligns with the common practice of using timestamps as unique identifiers in databases. A successful record creation would mean that SurrealDB correctly interprets and stores the datetime value as the record's ID, allowing for efficient retrieval and manipulation of the data. This expectation is crucial for developers who rely on datetime-based IDs for their applications. The failure to create the record indicates a bug in the SurrealDB's ID handling mechanism.
SurrealDB Version
This issue has been observed in SurrealDB version 2.3.6 for Linux on x86_64. This information is essential for the SurrealDB team to identify and address the bug in subsequent releases. Knowing the specific version helps narrow down the potential causes and ensures that the fix is targeted and effective. Developers encountering this issue should be aware of the affected version and consider workarounds or updates when available. The SurrealDB version is a key factor in diagnosing and resolving this problem.
Understanding the Issue
The core of the problem lies in how SurrealDB handles datetime values as record IDs. While SurrealDB supports datetime data types, it appears there's a discrepancy in how these values are processed when used as IDs. The error message suggests that the datetime value is being recognized but not accepted as a valid ID format. This could be due to internal validation checks or data type conversions within the database engine. Understanding the root cause requires a deeper dive into the SurrealDB codebase, specifically the ID generation and validation logic. This issue highlights the importance of rigorous testing and validation of data type handling in database systems.
Potential Causes
Several factors could contribute to this issue:
-
Data Type Mismatch: There might be a mismatch between the declared data type of the
id
field (datetime) and the expected data type for record IDs in SurrealDB. Although the field is defined as datetime, the internal ID representation might require a specific format or type that is not directly compatible with the output oftime::now()
. This discrepancy can lead to the validation failure. -
ID Generation Logic: The internal logic for generating and validating IDs might not correctly handle datetime values. The algorithm might be expecting a different format or a specific type of value, causing the datetime value to be rejected. This requires a review of the ID generation and validation code within SurrealDB.
-
String Conversion: The datetime value might be implicitly converted to a string representation, and this string format might not be recognized as a valid ID. Different string formats for datetimes can lead to parsing issues and validation failures. Ensuring consistent formatting is crucial for correct ID handling.
-
Underlying Bug: There could be an underlying bug in the SurrealDB engine that specifically affects datetime IDs. This could be related to how the database stores and retrieves IDs, or how it indexes records based on datetime values. Identifying such bugs requires thorough debugging and analysis of the database's internal operations.
Possible Solutions and Workarounds
While a definitive solution requires a fix from the SurrealDB team, here are some potential workarounds and solutions:
-
Use a Different ID Type: Consider using a different data type for the ID, such as a UUID or an auto-incrementing integer. UUIDs are universally unique identifiers that are well-suited for database IDs. Auto-incrementing integers can also provide unique IDs, but they may not be suitable for distributed systems. This workaround avoids the datetime ID issue altogether.
-
Convert Datetime to String: Convert the datetime value to a string format that is accepted by SurrealDB. This might involve formatting the datetime value in a specific way that aligns with SurrealDB's ID validation rules. However, this approach might impact performance and sorting capabilities.
-
Use a Hash of the Datetime: Generate a hash of the datetime value and use the hash as the ID. This ensures uniqueness and compatibility with ID requirements. However, it's important to choose a suitable hashing algorithm to minimize collisions.
-
Wait for a Fix: Monitor the SurrealDB issue tracker and wait for an official fix from the SurrealDB team. This is the most reliable solution, as it ensures that the issue is addressed at the core of the database engine. In the meantime, use one of the workarounds to continue development.
Conclusion
The SurrealDB datetime ID issue highlights the importance of careful data type handling and ID validation in database systems. While this bug prevents the use of datetime values as IDs in version 2.3.6, understanding the potential causes and workarounds can help developers mitigate the problem. By reporting the bug and exploring potential solutions, the community can contribute to the improvement of SurrealDB and ensure its reliability for future applications. The SurrealDB team's attention to this issue will be crucial in resolving it and providing a robust solution for handling datetime IDs.