mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
throw when deletePreviousAnyToOneRelations and deletePreviousOneToAnyRelations are called for non compatible relations
This commit is contained in:
parent
8248458a1f
commit
bf16d07463
@ -27,6 +27,7 @@ const { deleteRelatedMorphOneRelationsAfterMorphToManyUpdate } = require('./morp
|
||||
const {
|
||||
isBidirectional,
|
||||
isAnyToOne,
|
||||
isOneToAny,
|
||||
hasOrderColumn,
|
||||
hasInverseOrderColumn,
|
||||
} = require('../metadata/relations');
|
||||
@ -516,7 +517,9 @@ const createEntityManager = (db) => {
|
||||
const relsToAdd = cleanRelationData.set || cleanRelationData.connect;
|
||||
const relIdsToadd = toIds(relsToAdd);
|
||||
|
||||
await deletePreviousOneToAnyRelations({ id, attribute, relIdsToadd, db });
|
||||
if (isBidirectional(attribute) && isOneToAny(attribute)) {
|
||||
await deletePreviousOneToAnyRelations({ id, attribute, relIdsToadd, db });
|
||||
}
|
||||
|
||||
// prepare new relations to insert
|
||||
const insert = relsToAdd.map((data) => {
|
||||
@ -909,15 +912,17 @@ const createEntityManager = (db) => {
|
||||
}
|
||||
|
||||
// Delete the previous relations for oneToAny relations
|
||||
if (!isEmpty(relIdsToaddOrMove)) {
|
||||
if (isBidirectional(attribute) && isOneToAny(attribute)) {
|
||||
await deletePreviousOneToAnyRelations({
|
||||
id,
|
||||
attribute,
|
||||
relIdsToadd: relIdsToaddOrMove,
|
||||
db,
|
||||
});
|
||||
}
|
||||
|
||||
// Delete the previous relations for anyToOne relations
|
||||
// Delete the previous relations for anyToOne relations
|
||||
if (isBidirectional(attribute) && isAnyToOne(attribute)) {
|
||||
await deletePreviousAnyToOneRelations({
|
||||
id,
|
||||
attribute,
|
||||
|
||||
@ -20,23 +20,24 @@ const { createQueryBuilder } = require('../query');
|
||||
* @param {string} params.db - database instance
|
||||
*/
|
||||
const deletePreviousOneToAnyRelations = async ({ id, attribute, relIdsToadd, db }) => {
|
||||
if (!(isBidirectional(attribute) && isOneToAny(attribute))) {
|
||||
throw new Error(
|
||||
'deletePreviousOneToAnyRelations can only be called for bidirectional oneToAny relations'
|
||||
);
|
||||
}
|
||||
const { joinTable } = attribute;
|
||||
const { joinColumn, inverseJoinColumn } = joinTable;
|
||||
|
||||
// need to delete the previous relations for oneToAny relations
|
||||
if (isBidirectional(attribute) && isOneToAny(attribute)) {
|
||||
// delete previous oneToAny relations
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[inverseJoinColumn.name]: relIdsToadd,
|
||||
[joinColumn.name]: { $ne: id },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[inverseJoinColumn.name]: relIdsToadd,
|
||||
[joinColumn.name]: { $ne: id },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
|
||||
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToadd });
|
||||
}
|
||||
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToadd });
|
||||
};
|
||||
|
||||
/**
|
||||
@ -51,46 +52,46 @@ const deletePreviousAnyToOneRelations = async ({ id, attribute, relIdToadd, db }
|
||||
const { joinTable } = attribute;
|
||||
const { joinColumn, inverseJoinColumn } = joinTable;
|
||||
|
||||
// Delete the previous relations for anyToOne relations
|
||||
if (isBidirectional(attribute) && isAnyToOne(attribute)) {
|
||||
// update orders for previous anyToOne relations that will be deleted if it has order (manyToOne)
|
||||
if (!(isBidirectional(attribute) && isAnyToOne(attribute))) {
|
||||
throw new Error(
|
||||
'deletePreviousAnyToOneRelations can only be called for bidirectional anyToOne relations'
|
||||
);
|
||||
}
|
||||
// handling manyToOne
|
||||
if (isManyToAny(attribute)) {
|
||||
// if the database integrity was not broken relsToDelete is supposed to be of length 1
|
||||
const relsToDelete = await createQueryBuilder(joinTable.name, db)
|
||||
.select(inverseJoinColumn.name)
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
|
||||
// handling manyToOne
|
||||
if (isManyToAny(attribute)) {
|
||||
// if the database integrity was not broken relsToDelete is supposed to be of length 1
|
||||
const relsToDelete = await createQueryBuilder(joinTable.name, db)
|
||||
.select(inverseJoinColumn.name)
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
const relIdsToDelete = map(inverseJoinColumn.name, relsToDelete);
|
||||
|
||||
const relIdsToDelete = map(inverseJoinColumn.name, relsToDelete);
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $in: relIdsToDelete },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $in: relIdsToDelete },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToDelete });
|
||||
|
||||
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToDelete });
|
||||
|
||||
// handling oneToOne
|
||||
} else {
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
}
|
||||
// handling oneToOne
|
||||
} else {
|
||||
await createQueryBuilder(joinTable.name, db)
|
||||
.delete()
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user