From 0964b42ba44bfc908e618b1aa0c9069cf2fb6e5a Mon Sep 17 00:00:00 2001 From: Rijk van Zanten Date: Thu, 3 Jun 2021 16:32:39 -0400 Subject: [PATCH] Drop FK based on constraint name instead of column (#6042) Fixes #6029 --- api/src/services/relations.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/api/src/services/relations.ts b/api/src/services/relations.ts index 89190a23cd..53a7399669 100644 --- a/api/src/services/relations.ts +++ b/api/src/services/relations.ts @@ -201,15 +201,19 @@ export class RelationsService { await this.knex.transaction(async (trx) => { if (existingRelation.related_collection) { await trx.schema.alterTable(collection, async (table) => { + // undefined == default to knex's default + let constraintName: string | null | undefined = undefined; + // If the FK already exists in the DB, drop it first if (existingRelation?.schema) { - table.dropForeign(field); + constraintName = existingRelation.schema.constraint_name; + table.dropForeign(field, existingRelation.schema.constraint_name || undefined); } this.alterType(table, relation); table - .foreign(field) + .foreign(field, constraintName || undefined) .references( `${existingRelation.related_collection!}.${ this.schema.collections[existingRelation.related_collection!].primary @@ -261,7 +265,7 @@ export class RelationsService { await this.knex.transaction(async (trx) => { if (existingRelation.schema) { await trx.schema.alterTable(existingRelation.collection, (table) => { - table.dropForeign(existingRelation.field); + table.dropForeign(existingRelation.field, existingRelation.schema?.constraint_name || undefined); }); }