Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lpbonomi opened this issue Feb 1, 2024 · 5 comments
Open

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

lpbonomi opened this issue Feb 1, 2024 · 5 comments

Comments

@lpbonomi
Copy link

lpbonomi commented Feb 1, 2024

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
Copy link
Collaborator

dziraf commented Feb 1, 2024

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
Copy link
Author

lpbonomi commented Feb 1, 2024

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
Copy link
Collaborator

dziraf commented Feb 1, 2024

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
Copy link
Author

lpbonomi commented Feb 1, 2024

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
Copy link
Collaborator

dziraf commented Feb 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants