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 <aiden.foxx@sbab.se>
This commit is contained in:
Aiden Foxx
2021-06-17 21:43:32 +02:00
committed by rijkvanzanten
parent 10101073a7
commit 1da4751141
2 changed files with 19 additions and 14 deletions

View File

@@ -83,9 +83,6 @@ export async function up(knex: Knex): Promise<void> {
}
}
// 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<void> {
}
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(

View File

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