Can I turn an existing SurrealDB table into a RELATION table?
Existing table changed to TYPE RELATION
Warning: the statement succeeds, then writes fail, access changes, or the table is held up.
What happens
Observed on SurrealDB 3.0.2: after DEFINE TABLE OVERWRITE follows TYPE RELATION IN user OUT user, UPDATE of a record stored before fails with "Couldn't coerce value for field `in` of `follows:1`: Expected `record<user>` but found `NONE`".
Why it is dangerous on a populated table
SurrealDB checks a schema change against new writes, not the records already stored, so an empty table hides it: on a populated one every old record is a write waiting to fail, and whole-table work runs for the length of the table in one transaction.
Fires on
ALTER TABLE follows TYPE RELATION IN user OUT user;The safe pattern
Create a new relation table, copy the edges into it with RELATE in batches, move the code over, and remove the old table in a later migration.
DEFINE TABLE follows_v2 TYPE RELATION IN user OUT user;
-- batched job: RELATE each (from, to) pair of follows into follows_v2Fixtures
The rule ships with these files and the test suite runs them on every change: the first set must fire, the second must stay silent.
Fires (2)
ALTER TABLE follows TYPE RELATION IN user OUT user;DEFINE TABLE OVERWRITE follows TYPE RELATION FROM user TO user PERMISSIONS NONE;Stays silent (2)
ALTER TABLE follows TYPE NORMAL;DEFINE TABLE follows TYPE RELATION IN user OUT user;How to check locally
SurrealDB support is in beta: this rule runs locally in the free CLI over .surql migrations, static only, and not in the hosted service yet. No install, nothing leaves your machine:
npx bolvrk check migration.surql --engine=surrealdb