Drop FK based on constraint name instead of column (#6042)

Fixes #6029
This commit is contained in:
Rijk van Zanten
2021-06-03 16:32:39 -04:00
committed by GitHub
parent 1cdbc91593
commit 0964b42ba4

View File

@@ -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);
});
}