SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.07k stars 650 forks source link

[Bug]: @adminjs/relations - OneToMany issue - NestJS + Prisma #1595

Closed vogloblinsky closed 6 months ago

vogloblinsky commented 7 months ago

Contact Details

No response

What happened?

I am trying to reproduce the example of the documentation : https://docs.adminjs.co/basics/features/relations

I have this prisma model :

generator client {
    provider = "prisma-client-js"
}

datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
}

model Organization {
    id      Int      @id @default(autoincrement())
    name    String
    persons Person[]
}

model Person {
    id             Int          @id @default(autoincrement())
    name           String
    email          String
    organization   Organization @relation(fields: [organizationId], references: [id])
    organizationId Int
}

and these two definitions for AdminJS :

export const createPersonResource = () => ({
  resource: {
    model: getModelByName('Person'),
    client: prisma,
  },
  features: [targetRelationSettingsFeature()],
});
export const createOrganizationResource = () => ({
  resource: {
    model: getModelByName('Organization'),
    client: prisma,
  },
  features: [
    owningRelationSettingsFeature({
      componentLoader,
      licenseKey: process.env.ADMINJS_LICENSE_KEY,
      relations: {
        persons: {
          type: RelationType.OneToMany,
          target: {
            joinKey: 'organizationId',
            resourceId: 'Person',
          },
        },
      },
    }),
  ],
});

When i open a detail page of an Organization, i have this error in the terminal (log output below)

Bug prevalence

Everytime

AdminJS dependencies version

"@adminjs/express": "^6.1.0", "@adminjs/nestjs": "^6.1.0", "@adminjs/prisma": "^5.0.1", "@adminjs/relations": "^1.0.0", "@nestjs/common": "^10.3.0", "@nestjs/config": "^3.1.1", "@nestjs/core": "^10.3.0", "@nestjs/platform-express": "^10.3.0", "@prisma/client": "^5.7.1", "adminjs": "^7.5.2", "express-formidable": "^1.2.0", "express-session": "^1.17.3", "pg": "latest", "reflect-metadata": "^0.2.1", "rxjs": "^7.8.1"

What browsers do you see the problem on?

image

Relevant log output

[Nest] 8336  - 20/12/2023 16:41:21   ERROR [ExceptionsHandler] Cannot read properties of null (reading 'type')
TypeError: Cannot read properties of null (reading 'type')
    at file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/@adminjs/prisma/src/utils/converters.ts:40:59
    at Array.reduce (<anonymous>)
    at convertFilter (file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/@adminjs/prisma/src/utils/converters.ts:39:31)
    at Resource.find (file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/@adminjs/prisma/src/Resource.ts:70:13)
    at oneToManyHandler (file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/@adminjs/relations/lib/actions/one-to-many/one-to-many.handler.js:1:623)
    at file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/@adminjs/relations/lib/actions/relations.handler.js:1:727
    at file:///Users/vincent/Documents/www/Orange/marathon-pour-tous/adminjs-debug-relations-nestjs-prisma/node_modules/adminjs/lib/backend/decorators/action/action-decorator.js:98:90

Relevant code that's giving you issues

In the convertFilter function, if i log the filters i got this :

{
  organizationId: { path: 'organizationId', property: null, value: '2' }
}
vogloblinsky commented 7 months ago

Additional informations : same bug with TypeOrm, but fixed adding @Column around organizationId inside Person class entity.

dziraf commented 6 months ago

Just checked this in a test project and it'd worked for me when I used organization instead of organizationId for joinKey. Looks like @adminjs/prisma uses relation fields instead of FKs to query relationships.

ingerman20 commented 4 months ago

Additional informations : same bug with TypeOrm, but fixed adding @column around organizationId inside Person class entity.

Thanks a lot! It's definitely works!