Skip to content

Instantly share code, notes, and snippets.

@kearfy
Created April 14, 2023 10:10
Show Gist options
  • Select an option

  • Save kearfy/5d081644507dba937dc3183b201b34cd to your computer and use it in GitHub Desktop.

Select an option

Save kearfy/5d081644507dba937dc3183b201b34cd to your computer and use it in GitHub Desktop.
How to "rename" a field in SurrealDB
BEGIN;
-- Starting point
DEFINE TABLE test SCHEMAFULL;
DEFINE FIELD one ON test;
CREATE test:1 SET one = 123;
CREATE test:2 SET one = 456;
-- "rename" the field
DEFINE FIELD two ON test;
UPDATE test SET two = one;
REMOVE FIELD one ON test;
UPDATE test;
-- Store the result
LET $result = SELECT * FROM test;
-- cleanup for test
REMOVE TABLE test;
REMOVE FIELD two ON test;
DELETE test;
-- return the data
RETURN $result;
COMMIT;
@joakim
Copy link

joakim commented Dec 8, 2024

Thank you, but wow, that's convoluted for such a simple task. A UX flaw in SurrealDB, IMO.

It's a one-liner in SQL:

ALTER TABLE test RENAME COLUMN one TO two;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment