diff --git a/packages/core/database/lib/entity-manager/index.js b/packages/core/database/lib/entity-manager/index.js index 107f47a129..9248fb730d 100644 --- a/packages/core/database/lib/entity-manager/index.js +++ b/packages/core/database/lib/entity-manager/index.js @@ -416,6 +416,10 @@ const createEntityManager = (db) => { const { idColumn, typeColumn } = morphColumn; + if (isEmpty(cleanRelationData.set)) { + continue; + } + const rows = cleanRelationData.set.map((data, idx) => { return { [joinColumn.name]: data.id, @@ -428,10 +432,6 @@ const createEntityManager = (db) => { }; }); - if (isEmpty(rows)) { - continue; - } - await this.createQueryBuilder(joinTable.name).insert(rows).execute(); } @@ -445,6 +445,10 @@ const createEntityManager = (db) => { const { idColumn, typeColumn, typeField = '__type' } = morphColumn; + if (isEmpty(cleanRelationData.set)) { + continue; + } + const rows = cleanRelationData.set.map((data, idx) => ({ [joinColumn.name]: id, [idColumn.name]: data.id, @@ -454,10 +458,6 @@ const createEntityManager = (db) => { order: idx + 1, })); - if (isEmpty(rows)) { - continue; - } - // delete previous relations await deleteRelatedMorphOneRelationsAfterMorphToManyUpdate(rows, { uid, @@ -628,6 +628,10 @@ const createEntityManager = (db) => { }) .execute(); + if (isEmpty(cleanRelationData.set)) { + continue; + } + const rows = cleanRelationData.set.map((data, idx) => ({ [joinColumn.name]: data.id, [idColumn.name]: id, @@ -638,10 +642,6 @@ const createEntityManager = (db) => { field: attributeName, })); - if (isEmpty(rows)) { - continue; - } - await this.createQueryBuilder(joinTable.name).insert(rows).execute(); } @@ -667,6 +667,10 @@ const createEntityManager = (db) => { }) .execute(); + if (isEmpty(cleanRelationData.set)) { + continue; + } + const rows = cleanRelationData.set.map((data, idx) => ({ [joinColumn.name]: id, [idColumn.name]: data.id, @@ -676,10 +680,6 @@ const createEntityManager = (db) => { order: idx + 1, })); - if (isEmpty(rows)) { - continue; - } - // delete previous relations await deleteRelatedMorphOneRelationsAfterMorphToManyUpdate(rows, { uid,