diff --git a/api/src/services/payload.ts b/api/src/services/payload.ts index 1ff86ffe45..7e21c9a76d 100644 --- a/api/src/services/payload.ts +++ b/api/src/services/payload.ts @@ -301,11 +301,10 @@ export class PayloadService { const relatedRecord: Partial = payload[relation.many_field]; const hasPrimaryKey = relatedRecord.hasOwnProperty(relation.one_primary); - let relatedPrimaryKey: PrimaryKey; - - if (hasPrimaryKey) { - relatedPrimaryKey = relatedRecord[relation.one_primary]; + let relatedPrimaryKey: PrimaryKey = relatedRecord[relation.one_primary]; + const exists = hasPrimaryKey && !!(await itemsService.readByKey(relatedPrimaryKey)); + if (exists) { if (relatedRecord.hasOwnProperty('$delete') && relatedRecord.$delete) { await itemsService.delete(relatedPrimaryKey); } else { diff --git a/app/src/interfaces/many-to-many/is-new.ts b/app/src/interfaces/many-to-many/is-new.ts deleted file mode 100644 index f735b2c856..0000000000 --- a/app/src/interfaces/many-to-many/is-new.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Ref } from '@vue/composition-api'; -import { Relation } from '@/types/'; -import { Field } from '@/types'; - -type IsNewContext = { - relationCurrentToJunction: Ref; - junctionCollectionPrimaryKeyField: Ref; - relatedCollectionPrimaryKeyField: Ref; -}; - -export default function isNew( - item: any, - { relationCurrentToJunction, junctionCollectionPrimaryKeyField, relatedCollectionPrimaryKeyField }: IsNewContext -) { - if (!relationCurrentToJunction.value) return; - if (!relationCurrentToJunction.value.junction_field) return; - - const junctionPrimaryKey = junctionCollectionPrimaryKeyField.value.field; - const junctionField = relationCurrentToJunction.value.junction_field; - const relatedPrimaryKey = relatedCollectionPrimaryKeyField.value.field; - const hasPrimaryKey = !!item[junctionPrimaryKey]; - const hasRelatedPrimaryKey = !!item[junctionField]?.[relatedPrimaryKey]; - - return hasPrimaryKey === false && hasRelatedPrimaryKey === false; -} diff --git a/app/src/interfaces/many-to-many/many-to-many.vue b/app/src/interfaces/many-to-many/many-to-many.vue index 8f68791697..488e8cd4e5 100644 --- a/app/src/interfaces/many-to-many/many-to-many.vue +++ b/app/src/interfaces/many-to-many/many-to-many.vue @@ -16,7 +16,7 @@