如何找到关系和子关系?

How to find relations and sub-relations?

我是 TypeOrm 和 NestJS 的新手。你能建议我如何在我的代码中获得关系和子关系吗?我不明白如何获得所有关系 Board->Columns-Cards。谢谢

Entity({ name: 'boards' })
export class Board {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Clmn({ type: 'varchar', length: 120 })
  public name: string;

  @CreateDateColumn({ type: 'timestamp' })
  public created_at!: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  public updated_at!: Date;

  @OneToMany('Column', (column: Column) => column.board_id, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  columns: Array<Column>;
}

@Entity({ name: 'columns' })
export class Column {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Colmn({ type: 'varchar', length: 120 })
  public name: string;

  @CreateDateColumn({ type: 'timestamp' })
  public created_at!: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  public updated_at!: Date;

  @ManyToOne(() => Board, (board: Board) => board.columns)
  @JoinColumn({ name: 'board_id' })
  board_id: number;

  @OneToMany('Card', (card: Card) => card.column_id, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  columns: Array<Column>;
}

@Entity({ name: 'cards' })
export class Card {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Colmn({ type: 'varchar', length: 120 })
  public name: string;

  @Colmn({ type: 'varchar', length: 120 })
  public text: string;

  @CreateDateColumn({ type: 'timestamp' })
  public created_at!: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  public updated_at!: Date;

  @ManyToOne(() => Column, (card: Column) => card.columns)
  @JoinColumn({ name: 'column_id' })
  column_id: number;
}

我检查堆栈溢出中的所有文档和答案?但不明白如何决定这个问题。我尝试使用此代码会导致错误:未找到关系“columns.card”;请检查它是否正确并且确实存在于您的实体中。

public async getBoardWithContent(board_id: number) {
    return await this.boardsRepository.find({
      where: { id: board_id },
      relations: ['columns', 'columns.card'],
    });
  }

您的 Column.columns 属性很奇怪,您的模型中似乎混淆了卡片和列。

我觉得应该是:

@OneToMany(() => Card, (card: Card) => card.column_id, {
  onDelete: 'CASCADE',
  onUpdate: 'CASCADE',
})
cards: Array<Card>;

然后。请求:

public async getBoardWithContent(board_id: number) {
  return await this.boardsRepository.find({
    where: { id: board_id },
    relations: ['columns', 'columns.cards'],
  });
}

应该可以。