{"record":{"id":"9e99ec34a3e88049","repo":"Budibase/budibase","slug":"unable-to-generate-foreign-key-column-foreignk","errorCode":null,"errorMessage":"Unable to generate foreign key - column ${foreignKey} already in use.","messagePattern":"Unable to generate foreign key - column (.+?) already in use\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/server/src/sdk/workspace/tables/external/index.ts","lineNumber":224,"sourceCode":"        throw new Error(\n          \"Junction table already exists, cannot create another relationship.\"\n        )\n      }\n      tables[junctionTable.name] = junctionTable\n      extraTablesToUpdate.push(junctionTable)\n    } else {\n      const fkTable =\n        relationType === RelationshipType.ONE_TO_MANY\n          ? tableToSave\n          : relatedTable\n      const foreignKey = generateLinkSchema(\n        schema,\n        tableToSave,\n        relatedTable,\n        relationType\n      )\n      if (fkTable.schema[foreignKey] != null) {\n        throw new Error(\n          `Unable to generate foreign key - column ${foreignKey} already in use.`\n        )\n      }\n      fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey)\n      if (fkTable.constrained == null) {\n        fkTable.constrained = []\n      }\n      if (fkTable.constrained.indexOf(foreignKey) === -1) {\n        fkTable.constrained.push(foreignKey)\n      }\n      // foreign key is in other table, need to save it to external\n      if (fkTable._id !== tableToSave._id) {\n        extraTablesToUpdate.push(fkTable)\n      }\n    }\n    generateRelatedSchema(schema, relatedTable, tableToSave, relatedColumnName)\n    tables[relatedTable.name] = relatedTable\n    schema.main = true","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/tables/external/index.ts#L206-L242","documentation":"When generating one-to-many/many-to-one link schema, save() computes a foreign key column name for the related table. If that column name already exists in the target table's schema, adding it would overwrite an existing column, so it throws with the conflicting column name.","triggerScenarios":"Creating a relationship whose generated foreign key column name collides with an existing column in the foreign-key table (fkTable.schema[foreignKey] != null) when calling save().","commonSituations":"A column with the same name already exists in the related SQL table; re-adding a relationship that previously added the FK column; default FK naming conventions colliding with business columns.","solutions":["Rename the existing column or the relationship field so the generated FK name is unique","Remove the old relationship column from the related table's schema before saving","Drop the stale FK column in the connected database and refresh entities","Choose explicit relationship column names if the connector supports it"],"exampleFix":"// before\nrelatedTable.schema[\"userId\"] already exists, relationship FK = \"userId\"\n// after\nrename field to \"owner\" so generated FK becomes \"owner_userId\" (unique)","handlingStrategy":"validation","validationCode":"const fkName = generateForeignKey(column, relatedTable)\nif (fkTable.schema[fkName] != null) {\n  throw new Error(`Column ${fkName} would collide with existing column`)\n}","typeGuard":"function fkColumnIsFree(schema: TableSchema, fk: string): boolean {\n  return schema[fk] == null\n}","tryCatchPattern":"try {\n  await tables.external.save(datasourceId, table)\n} catch (err) {\n  if (err.message.startsWith(\"Unable to generate foreign key\")) {\n    // rename the colliding column or field, then retry\n  }\n}","preventionTips":["Inspect the related table schema before defining relationships","Avoid business column names matching generated FK patterns","Clean up orphaned FK columns from removed relationships"],"tags":["external-datasource","foreign-key","naming-collision"],"backgroundTag":"column-name-collision","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}