Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Record IDs generated with rand() ulid() uuid() in custom functions generate same value #2260

Closed
2 tasks done
bsheldrick opened this issue Jul 14, 2023 · 0 comments · Fixed by #2469
Closed
2 tasks done
Assignees
Labels
bug Something isn't working topic:surrealql This is related to the SurrealQL query language

Comments

@bsheldrick
Copy link

Describe the bug

When using a random record id function to create a new record e.g. CREATE thing:ulid() in a custom function the value generated is the same on each function call. This results in a duplicate key error on subsequent calls.

Steps to reproduce

Define a function that creates records using a random function. In this example using ulid():

DEFINE FUNCTION fn::create_record_with_ulid($data: any) {
    LET $thing = CREATE thing:ulid() CONTENT {
        data: $data
    } RETURN AFTER;

    RETURN $thing;
};

Call the function wrapped in a transaction:

BEGIN TRANSACTION;
RETURN fn::create_record_with_ulid({ a: "b" });
COMMIT TRANSACTION;

The first call of the function will succeed. But any subsequent calls will produce an error:

Database record thing:01H5AB5KR7P6SCN9CV2E3B83AK already exists

This is the same behavior with the other random functions rand() and uuid(). I have only experienced this in custom functions.

A workaround to avoid this is to use the type::thing() function to create the record id in the custom function. For example.

DEFINE FUNCTION fn::create_record_with_ulid($data: any) {
    LET $id = type::thing("thing", rand::ulid());
    LET $thing = CREATE $id CONTENT {
        data: $data
    } RETURN AFTER;

    RETURN $thing;
};

Expected behaviour

I expect a new random record id to be generated when using rand() or ulid() or uuid() record id shortcuts to create records in a custom function.

SurrealDB version

1.0.0-beta.9+20230402.5eafebd for linux on x86_64

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bsheldrick bsheldrick added bug Something isn't working triage This issue is new labels Jul 14, 2023
@finnbear finnbear added the topic:surrealql This is related to the SurrealQL query language label Jul 14, 2023
@tobiemh tobiemh removed the triage This issue is new label Jul 14, 2023
@tobiemh tobiemh added this to the v1.0.0-beta.10 milestone Aug 19, 2023
@tobiemh tobiemh self-assigned this Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working topic:surrealql This is related to the SurrealQL query language
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants