'Why "join" in nestjs/crud not working with @ManyToOne relation?
I have two models with OneToMany relation. One client can have several rooms:
@Entity()
export class Room {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: false })
client_id: number;
@JoinColumn({ name: 'client_id' })
@ManyToOne(() => Client, client => client.rooms)
client: Client;
// ...
}
@Entity()
export class Client {
@PrimaryGeneratedColumn()
id: number;
@OneToMany(() => Room, room => room.client, { cascade: true })
rooms: Room[];
// ...
}
Also I did CRUD services and controllers like in documentation. Via curl I have posted data in those tables, here is all right. But when I try query room data with joined client - there are no client information in response:
http://localhost:3000/room?limit=1&cache=0&join=client||name,description
this query return only data from room table without client information
[{"id":1,"client_id":1,"name":"name 167","room":"room 164","price":447}]
What I did wrong, and how resolve this issue?
Thanks!
Solution 1:[1]
I didn't know anything about query of @Crud for controller.
@Crud({
model: { type: Room },
query: { // this resolved my issue
join: {
client: {
eager: true,
//exclude: ['password']
}
}
}
})
@Controller('room')
export class RoomController {
constructor(public service: RoomService) {}
}
Now all right. I hope this info will help someone
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tolyan |
