adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

[Bug]: Can not remove a ManyToMany relation

Open ImArxus opened this issue 1 year ago • 3 comments

Contact Details

No response

What happened?

Hello,

I want to delete a relation between two resources (ManyToMany), but I can only delete the associated record, as shown in the following picture. Did I miss something? Do you know if I need to have an additional configuration? Image

I tried to change the delete options in relation features configuration as follows, but here is the result: Image

Here is my relation features configuration:

features: [
    getUploadFeature(
      service.getBackgroundImageS3PathPrefix(),
      backgroundImageKey
    ),
    getUploadFeature(service.getOverviewImageS3PathPrefix(), overviewImageKey),
    owningRelationSettingsFeature({
      componentLoader,
      licenseKey: process.env.LICENSE_KEY as string,
      relations: {
        stores: {
          type: RelationType.ManyToMany,
          junction: {
            joinKey: "offerId",
            inverseJoinKey: "storeId",
            throughResourceId: "StoreOffer"
          },
          target: {
            resourceId: "Store"
          },
          deleteOptions: {
            enableDeleteRelation: true,
            enableDeleteRelatedRecord: false
          }
        }
      }
    }),
    targetRelationSettingsFeature()
]

I also have a custom handler for delete action, maybe it can cause an issue:

actionType: "record",
    before: async (request) => {
      const recordId = request.params.recordId;
      if (recordId) {
        await service.delete(recordId);
      }
      return request;
    },
    icon: "Trash",
    after: async (
      response: ActionResponse,
      _request: ActionRequest,
      context: ActionContext
    ) => {
      const resourceName = context.resource.id();
      const message = `The ${resourceName} was successfully removed`;
      response.notice = {
        message: message,
        type: "success"
      };
      response.redirectUrl = context.h.resourceUrl({
        resourceId: resourceName
      });

      return errorHandler(response);
}

And here are the request to create the relation table:

CREATE TABLE "store_offer" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, "storeId" uuid NOT NULL, "offerId" uuid NOT NULL, CONSTRAINT "PK_d337e2be4dd1445aa51f9641ad7" PRIMARY KEY ("id"))
CREATE UNIQUE INDEX "unique_constraint_store_offer" ON "store_offer" ("offerId", "storeId") WHERE "deletedAt" IS NULL

Thanks

Bug prevalence

Always

AdminJS dependencies version

"dependencies": {
    "@adminjs/express": "^6.1.0",
    "@adminjs/relations": "^1.1.2",
    "@adminjs/typeorm": "^5.0.1",
    "@adminjs/upload": "^4.0.2",
    "adminjs": "^7.8.12"
}

What browsers do you see the problem on?

Chrome

Relevant log output


Relevant code that's giving you issues


ImArxus avatar Feb 13 '25 18:02 ImArxus

Any updates on this one? Im running into the same issue.

martinfr-dev avatar May 05 '25 17:05 martinfr-dev

@martinfr-dev if you downgrade to "@adminjs/relations": "^1.1.0" the problem does not yet exist

ImArxus avatar May 05 '25 17:05 ImArxus

Nice catch, thanks! Would be great to understand what issue is but seems to be working now.

martinfr-dev avatar May 05 '25 17:05 martinfr-dev