{"record":{"id":"06119dd33dece368","repo":"beekeeper-studio/beekeeper-studio","slug":"surrealdb-does-not-use-traditional-foreign-key-con","errorCode":null,"errorMessage":"SurrealDB does not use traditional foreign key constraints. Modify field definitions instead.","messagePattern":"SurrealDB does not use traditional foreign key constraints\\. Modify field definitions instead\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/studio/src/shared/lib/sql/change_builder/SurrealDBChangeBuilder.ts","lineNumber":127,"sourceCode":"    if (!drops?.length) return null;\n\n    return drops.map(spec => \n      `REMOVE INDEX ${this.wrapIdentifier(spec.name)} ON TABLE ${this.wrapIdentifier(this.table)}`\n    ).join('; ');\n  }\n\n  // SurrealDB uses record links instead of foreign keys\n  singleRelation(spec: CreateRelationSpec): string {\n    const fromColumn = this.wrapIdentifier(spec.fromColumn);\n    const toTable = this.wrapIdentifier(spec.toTable);\n    \n    // In SurrealDB, we define a field that references another table\n    return `DEFINE FIELD ${fromColumn} ON TABLE ${this.wrapIdentifier(this.table)} TYPE record<${toTable}>`;\n  }\n\n  // SurrealDB doesn't have traditional foreign key constraints to drop\n  dropRelations(_names: string[]): string | null {\n    throw new Error('SurrealDB does not use traditional foreign key constraints. Modify field definitions instead.');\n  }\n\n  // Override the main alterTable method to handle SurrealDB's limitations\n  alterTable(spec: AlterTableSpec): string {\n    const statements: string[] = [];\n\n    // Handle column additions\n    if (spec.adds?.length) {\n      statements.push(...this.addColumns(spec.adds));\n    }\n\n    // Handle column drops\n    if (spec.drops?.length) {\n      statements.push(...this.dropColumns(spec.drops));\n    }\n\n    // Handle column alterations (type changes, defaults)\n    if (spec.alterations?.length) {","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/beekeeper-studio/beekeeper-studio/blob/4e3e03e3223b2b71a1af8926d455f533b80af837/apps/studio/src/shared/lib/sql/change_builder/SurrealDBChangeBuilder.ts#L109-L145","documentation":"SurrealDB has no traditional FOREIGN KEY constraints to drop; relationships are expressed as record<...> typed fields. SurrealDBChangeBuilder.dropRelations() always throws, directing callers to change the field definitions instead.","triggerScenarios":"Any call to dropRelations(names) — e.g. an alter-table flow that removes FK constraints from a SurrealDB table, or a generic 'drop constraint' action in the schema editor.","commonSituations":"Running shared constraint-removal code paths written for SQL databases against SurrealDB; porting migration scripts with 'DROP CONSTRAINT' statements; cleaning up FKs before a table drop.","solutions":["Redefine the referencing field's type (e.g. 'DEFINE FIELD other_id ON TABLE t TYPE string' or REMOVE FIELD) instead of dropping a constraint.","Guard with a dialect check and skip dropRelations for SurrealDB connections.","If the goal is removing a relationship entirely, run 'REMOVE FIELD <field> ON TABLE <table>'."],"exampleFix":"// before\nbuilder.dropRelations(['fk_post_user'])\n// after\n\"REMOVE FIELD user ON TABLE post;\" // or redefine field TYPE","handlingStrategy":"fallback","validationCode":"if (dialect === 'surrealdb') {\n  // no FK constraints: redefine or remove the referencing field instead\n  return names.map(f => `REMOVE FIELD ${f} ON TABLE ${table}`).join('; ')\n}\nbuilder.dropRelations(names)","typeGuard":"const supportsForeignKeyDdl = (dialect: string) => dialect !== 'surrealdb'","tryCatchPattern":"try {\n  stmt = builder.dropRelations(names)\n} catch (e) {\n  if (e.message.includes('foreign key constraints')) {\n    stmt = redefineOrRemoveReferenceFields(names) // REMOVE FIELD or change TYPE record<T>\n  } else throw e\n}","preventionTips":["Hide FK constraint management for SurrealDB tables.","Model relationships as record<T> typed fields and edit those instead.","Branch migrations on dialect before touching constraints.","Document SurrealDB's constraint model for plugin/script authors."],"tags":["surrealdb","unsupported-feature","foreign-key"],"backgroundTag":"unsupported-database-feature","analyzedSha":"4e3e03e3223b2b71a1af8926d455f533b80af837","analyzedAt":"2026-08-31T23:21:23.379Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}