Don't allow same foreign keys

This commit is contained in:
Adria Navarro 2024-11-05 11:31:46 +01:00
parent 94d3466113
commit 6505f518c8
2 changed files with 8 additions and 1 deletions

View File

@ -195,7 +195,8 @@
throughTable, throughTable,
fromTable.primary[0], fromTable.primary[0],
throughToKey throughToKey
), ) ||
errorChecker.differentColumns(throughFromKey, throughToKey),
throughToKey: throughToKey:
errorChecker.manyForeignKeySet(throughToKey) || errorChecker.manyForeignKeySet(throughToKey) ||
errorChecker.manyTypeMismatch( errorChecker.manyTypeMismatch(

View File

@ -3,6 +3,7 @@ import { RelationshipType } from "@budibase/types"
const typeMismatch = "Column type of the foreign key must match the primary key" const typeMismatch = "Column type of the foreign key must match the primary key"
const columnBeingUsed = "Column name cannot be an existing column" const columnBeingUsed = "Column name cannot be an existing column"
const mustBeDifferentTables = "From/to/through tables must be different" const mustBeDifferentTables = "From/to/through tables must be different"
const mustBeDifferentColumns = "Foreign keys must be different"
const primaryKeyNotSet = "Please pick the primary key" const primaryKeyNotSet = "Please pick the primary key"
const throughNotNullable = const throughNotNullable =
"Ensure non-key columns are nullable or auto-generated" "Ensure non-key columns are nullable or auto-generated"
@ -83,6 +84,11 @@ export class RelationshipErrorChecker {
return error ? mustBeDifferentTables : null return error ? mustBeDifferentTables : null
} }
differentColumns(columnA, columnB) {
const error = columnA && columnB && columnA === columnB
return error ? mustBeDifferentColumns : null
}
columnBeingUsed(table, column, ogName) { columnBeingUsed(table, column, ogName) {
return isColumnNameBeingUsed(table, column, ogName) ? columnBeingUsed : null return isColumnNameBeingUsed(table, column, ogName) ? columnBeingUsed : null
} }