adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

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

Open Chambu311 opened this issue 2 years ago • 3 comments

Contact Details

[email protected]

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;
}

Chambu311 avatar Feb 09 '23 18:02 Chambu311

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

dziraf avatar Feb 10 '23 07:02 dziraf

thank you that worked

Chambu311 avatar Feb 10 '23 13:02 Chambu311

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'

Beluomini avatar Nov 12 '24 14:11 Beluomini