mirror of
https://github.com/directus/directus.git
synced 2026-01-30 23:58:15 -05:00
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:
committed by
rijkvanzanten
parent
10101073a7
commit
1da4751141
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user