SoftwareBrothers / adminjs-prisma

MIT License
47 stars 27 forks source link

@adminjs/relations can't use implicit relations from Prisma #50

Open lpbonomi opened 8 months ago

lpbonomi commented 8 months ago

I am using Prisma's implicit relations (https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations#implicit-many-to-many-relations) for a table that I want to use and I get the error: "Error: There are no resources with given id: "_assetRelation"", that is, the implicit table created by prisma.

When I try to add a resource for the table I get the error "Error: Could not find model: "_assetRelation" in Prisma's DMMF!"

dziraf commented 8 months ago

The documentation for @adminjs/relations clearly states that junction tables must be registered as resources in AdminJS since every feature has to be generic enough to work similarly with all adapters.

My guess is that your junction table actually exists in DMMF but uses a different, Prisma-created name.

With reference to: https://github.com/SoftwareBrothers/adminjs-prisma/blob/main/src/utils/get-model-by-name.ts

I'd try logging Prisma.dmmf.datamodel (dmmf) in your app to see if it's there. If it's not in dmmf.models but elsewhere, you can create a custom reference to it instead of using getModelByName.

Alternatively, you could explicitly define your junction table in your Prisma schema and it should appear in dmmf.models.

lpbonomi commented 8 months ago

Hi @dziraf, thanks for the quick reply!

I've printed the Prisma.dmmf.datamodel but the models for the implicit relations are nowhere to be found, I've also checked on the whole Prisma instance.

I've also tried passing manually a model, something like this:

export const createAssetRelationResource: ResourceWithOptions = {
  resource: {
    model: {
      name: "AssetRelation",
      dbName: "_assetRelation",
      fields: [
        {
          name: "A",
          kind: "scalar",
          isList: false,
          isRequired: true,
          isUnique: false,
          isId: false,
          isReadOnly: false,
          hasDefaultValue: false,
          type: "Int",
          default: undefined,
          isGenerated: false,
          isUpdatedAt: false,
        },
        {
          name: "A",
          kind: "scalar",
          isList: false,
          isRequired: true,
          isUnique: false,
          isId: false,
          isReadOnly: false,
          hasDefaultValue: false,
          type: "Int",
          default: undefined,
          isGenerated: false,
          isUpdatedAt: false,
        }
      ],
      primaryKey: null,
      uniqueFields: [],
      uniqueIndexes: [],
      isGenerated: false,
    },
    client: prisma,
  },
  options:{}
};

But I get a NoResourceAdapterError error.

Looks like the only way out is to create an explicit relationship, do you have an idea of anything else that I could try before doing this?

dziraf commented 8 months ago

I think the safest approach right now would be to define an explicit model for the junction table. I assumed Prisma creates virtual models anyway but it might be they're querying that table simply by table name and foreign keys, but this is something we cannot access in AdminJS since we're using model methods for querying.

I'll try to arrange some time for me to investigate this.

lpbonomi commented 8 months ago

That'd be great. I don't really know how AdminJS internals work, but I was thinking maybe the Prisma adapter could be modified to include implicit relations

dziraf commented 8 months ago

Prisma adapter could be modified to include implicit relations

That would only work if:

  1. We can access implicit relations metadata via DMMF
  2. Virtual/Implicit relations in Prisma have find, count etc. methods

which is something I'll have to check