SoftwareBrothers / adminjs

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

[Bug]: Foreign Keys Columns not displaying data unless relation has eager option set to true #1409

Open Chambu311 opened 1 year ago

Chambu311 commented 1 year ago

Contact Details

achambu@gmail.com

What happened?

I want to display my database in the Admin Js Panel, but the the Foreign Key columns dont display any data unless i set the eager option to true in the entity. Doing so also crashes the backend because the eager option brings a huge amount of data. The docs say that specifying a RelationId in the entity is all it takes to fetch the ids of the other tables but that doesnt work. Also the filter option by foreign key doesnt work. It always takes the key as a 0.

Bug prevalence

When i want to display tables in Admin Panel

AdminJS dependencies version

"@adminjs/express": "^5.1.0", "@adminjs/nestjs": "^5.1.0", "@adminjs/typeorm": "^4.0.0", "typeorm": "^0.3.11",

What browsers do you see the problem on?

No response

Relevant log output

[Nest] 247  - 02/09/2023, 6:09:29 PM   ERROR [ExceptionsHandler] Property "0" was not found in "WhatsappLine". Make sure your query is correct.
EntityPropertyNotFoundError: Property "0" was not found in "WhatsappLine". Make sure your query is correct.
    at SelectQueryBuilder.buildWhere (/usr/src/app/node_modules/typeorm/query-builder/SelectQueryBuilder.js:2431:27)
    at SelectQueryBuilder.buildWhere (/usr/src/app/node_modules/typeorm/query-builder/SelectQueryBuilder.js:2576:48)
    at SelectQueryBuilder.applyFindOptions (/usr/src/app/node_modules/typeorm/query-builder/SelectQueryBuilder.js:1756:40)
    at SelectQueryBuilder.setFindOptions (/usr/src/app/node_modules/typeorm/query-builder/SelectQueryBuilder.js:69:14)
    at EntityManager.find (/usr/src/app/node_modules/typeorm/entity-manager/EntityManager.js:521:14)
    at Repository.find (/usr/src/app/node_modules/typeorm/repository/Repository.js:197:29)
    at Function.find (/usr/src/app/node_modules/typeorm/repository/BaseEntity.js:227:37)
    at Resource.<anonymous> (/usr/src/app/node_modules/@adminjs/typeorm/lib/Resource.js:63:48)
    at Generator.next (<anonymous>)
    at /usr/src/app/node_modules/@adminjs/typeorm/lib/Resource.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/src/app/node_modules/@adminjs/typeorm/lib/Resource.js:4:12)
    at Resource.find (/usr/src/app/node_modules/@adminjs/typeorm/lib/Resource.js:60:16)
    at Object.handler (/usr/src/app/node_modules/adminjs/lib/backend/actions/list/list-action.js:91:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ActionDecorator.handler (/usr/src/app/node_modules/adminjs/lib/backend/decorators/action/action-decorator.js:73:19)
    at async /usr/src/app/node_modules/@adminjs/express/lib/buildRouter.js:29:22

Relevant code that's giving you issues

export class Answer extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @OneToMany(() => QuestionAnswer, questionAnswer => questionAnswer.answer, { cascade: true })
  questionAnswers: QuestionAnswer[];

  @ManyToOne(() => Customer, customer => customer.answers, { onDelete: 'CASCADE', eager: true })
  customer: Customer;
  @RelationId((answer: Answer) => answer.customer)
  customerId: string;

  @ManyToOne(() => Survey, survey => survey.answer, { onDelete: 'CASCADE', eager: true })
  survey: Survey;
  @RelationId((answer: Answer) => answer.survey)
  surveyId: string;

  @CreateDateColumn()
  createdAt: Date;
}
dziraf commented 1 year ago

customerId and surveyId have to be defined with @Column decorator as regular columns. To your @ManyToOne relations add an additional @JoinColumn({ name: 'surveyId' }) decorator.

Chambu311 commented 1 year ago

thank you that worked

Beluomini commented 1 week ago

In my case I need to use the 'Relation':

  @ManyToOne(() => Entity, (entity) => entity.property, { eager: true })
  @JoinColumn({ name: "entity_id", referencedColumnName: "id" })
  entity: Relation<Entity>;

  @Column("text", { name: "entity_id", nullable: true})
  entity_id: string;

then it work to find, show, filter and edit the property 'entity_id'