Budibase/budibase · error · Error
Unable to generate foreign key - column ${foreignKey} alread
Error message
Unable to generate foreign key - column ${foreignKey} already in use. What it means
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.
Source
Thrown at packages/server/src/sdk/workspace/tables/external/index.ts:224
throw new Error(
"Junction table already exists, cannot create another relationship."
)
}
tables[junctionTable.name] = junctionTable
extraTablesToUpdate.push(junctionTable)
} else {
const fkTable =
relationType === RelationshipType.ONE_TO_MANY
? tableToSave
: relatedTable
const foreignKey = generateLinkSchema(
schema,
tableToSave,
relatedTable,
relationType
)
if (fkTable.schema[foreignKey] != null) {
throw new Error(
`Unable to generate foreign key - column ${foreignKey} already in use.`
)
}
fkTable.schema[foreignKey] = foreignKeyStructure(foreignKey)
if (fkTable.constrained == null) {
fkTable.constrained = []
}
if (fkTable.constrained.indexOf(foreignKey) === -1) {
fkTable.constrained.push(foreignKey)
}
// foreign key is in other table, need to save it to external
if (fkTable._id !== tableToSave._id) {
extraTablesToUpdate.push(fkTable)
}
}
generateRelatedSchema(schema, relatedTable, tableToSave, relatedColumnName)
tables[relatedTable.name] = relatedTable
schema.main = trueView on GitHub (pinned to a81a902e9a)
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
Example fix
// before relatedTable.schema["userId"] already exists, relationship FK = "userId" // after rename field to "owner" so generated FK becomes "owner_userId" (unique)
Defensive patterns
Strategy: validation
Validate before calling
const fkName = generateForeignKey(column, relatedTable)
if (fkTable.schema[fkName] != null) {
throw new Error(`Column ${fkName} would collide with existing column`)
} Type guard
function fkColumnIsFree(schema: TableSchema, fk: string): boolean {
return schema[fk] == null
} Try / catch
try {
await tables.external.save(datasourceId, table)
} catch (err) {
if (err.message.startsWith("Unable to generate foreign key")) {
// rename the colliding column or field, then retry
}
} Prevention
- Inspect the related table schema before defining relationships
- Avoid business column names matching generated FK patterns
- Clean up orphaned FK columns from removed relationships
When it happens
Trigger: 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().
Common situations: 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.
Related errors
- Junction table already exists, cannot create another relatio
- Table ID is unknown, cannot find table
- Cannot fetch row by ID "${rowId}"
- Unable to lookup relationships - undefined column properties
- unable to find related table
AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29).
Data as JSON: /api/errors/9e99ec34a3e88049.
Report an issue: GitHub.