From 1da47511412bd4efaa3f393ea74d4d2811a41f90 Mon Sep 17 00:00:00 2001 From: Aiden Foxx Date: Thu, 17 Jun 2021 21:43:32 +0200 Subject: [PATCH] Fixed invalid onDelete constraint for some schemas (#6308) * Fixed invalid onDelete clause for some schemas * Ran prettier * Updated all onDelete statements to be Oracle friendly Co-authored-by: Aiden Foxx --- .../20210518A-add-foreign-key-constraints.ts | 14 +++++++------- api/src/services/relations.ts | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/api/src/database/migrations/20210518A-add-foreign-key-constraints.ts b/api/src/database/migrations/20210518A-add-foreign-key-constraints.ts index 4456471531..d27efecc2e 100644 --- a/api/src/database/migrations/20210518A-add-foreign-key-constraints.ts +++ b/api/src/database/migrations/20210518A-add-foreign-key-constraints.ts @@ -83,9 +83,6 @@ export async function up(knex: Knex): Promise { } } - // Can't reliably have circular cascade - const action = constraint.many_collection === constraint.one_collection ? 'NO ACTION' : 'SET NULL'; - // MySQL doesn't accept FKs from `int` to `int unsigned`. `knex` defaults `.increments()` // to `unsigned`, but defaults `.integer()` to `int`. This means that created m2o fields // have the wrong type. This step will force the m2o `int` field into `unsigned`, but only @@ -104,12 +101,15 @@ export async function up(knex: Knex): Promise { } const indexName = getDefaultIndexName('foreign', constraint.many_collection, constraint.many_field); - - table + const builder = table .foreign(constraint.many_field, indexName) .references(relatedPrimaryKeyField) - .inTable(constraint.one_collection!) - .onDelete(action); + .inTable(constraint.one_collection!); + + // Can't reliably have circular cascade + if (constraint.many_collection !== constraint.one_collection) { + builder.onDelete('SET NULL'); + } }); } catch (err) { logger.warn( diff --git a/api/src/services/relations.ts b/api/src/services/relations.ts index 3ab3ffbe1d..c9a38c8463 100644 --- a/api/src/services/relations.ts +++ b/api/src/services/relations.ts @@ -161,13 +161,15 @@ export class RelationsService { this.alterType(table, relation); const constraintName: string = getDefaultIndexName('foreign', relation.collection!, relation.field!); - - table + const builder = table .foreign(relation.field!, constraintName) .references( `${relation.related_collection!}.${this.schema.collections[relation.related_collection!].primary}` - ) - .onDelete(relation.schema?.on_delete || 'NO ACTION'); + ); + + if (relation.schema?.on_delete) { + builder.onDelete(relation.schema.on_delete); + } }); } @@ -222,14 +224,17 @@ export class RelationsService { this.alterType(table, relation); - table + const builder = table .foreign(field, constraintName || undefined) .references( `${existingRelation.related_collection!}.${ this.schema.collections[existingRelation.related_collection!].primary }` - ) - .onDelete(relation.schema?.on_delete || 'NO ACTION'); + ); + + if (relation.schema?.on_delete) { + builder.onDelete(relation.schema.on_delete); + } }); }