I am new to TypeOrm and NestJS. Can you suggest to me how to get relations and sub relations in my code? I don`t understand how to get all relations Board->Columns-Cards. Thanks
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;
}
I check all docs and answers in stack overflow? but don`t understand how to decide this problem. I try this code this results in an error: Relation "columns.card" was not found; please check if it is correct and really exists in your entity.
public async getBoardWithContent(board_id: number) {
return await this.boardsRepository.find({
where: { id: board_id },
relations: ['columns', 'columns.card'],
});
}
Your Column.columns
attribute is very strange, you seem to be mixing up cards and columns in your model.
I think this should be:
@OneToMany(() => Card, (card: Card) => card.column_id, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
cards: Array<Card>;
Then. requesting:
public async getBoardWithContent(board_id: number) {
return await this.boardsRepository.find({
where: { id: board_id },
relations: ['columns', 'columns.cards'],
});
}
should work.