TypeORM 结果在 OneToOne 关系上未定义
TypeORM results to undefined on OneToOne relationship
我是 NestJS 的新手(我对 Angular 有一点经验)。当我 console.log 一个 JoinTable(表是新的。我的任务是创建一个新的微服务)时,我得到 undefined
所以Unit
和Hub
有One to One
关系。
这里是单位
@Entity({
name: 'units',
})
export class UnitEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(() => HubEntity)
@JoinColumn({name: "hub_id"})
hub: HubEntity;
仅供参考,我也试过这样编码:
@OneToOne(type => HubEntity, hub => hub.unit)
@JoinColumn({name: "hub_id"})
hub: HubEntity;
对于集线器:
@Entity({
name: 'hubs',
})
export class HubEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(type => UnitEntity, unit => unit.hub)
@JoinColumn({name: "hub_id"})
unit: UnitEntity;
为了测试,我是这样安慰它的。
async findAllByUnit(unitId: number) {
const unit = await this.unitRepository.findOne(unitId)
console.log(unit.hub)
}
它returnsundefined
请让我知道我遗漏了什么。
提前致谢~
你必须包括这样的关系:
await this.unitRepository.findOne(unitId, {
relations: ['hub']
})
我是 NestJS 的新手(我对 Angular 有一点经验)。当我 console.log 一个 JoinTable(表是新的。我的任务是创建一个新的微服务)时,我得到 undefined
所以Unit
和Hub
有One to One
关系。
这里是单位
@Entity({
name: 'units',
})
export class UnitEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(() => HubEntity)
@JoinColumn({name: "hub_id"})
hub: HubEntity;
仅供参考,我也试过这样编码:
@OneToOne(type => HubEntity, hub => hub.unit)
@JoinColumn({name: "hub_id"})
hub: HubEntity;
对于集线器:
@Entity({
name: 'hubs',
})
export class HubEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(type => UnitEntity, unit => unit.hub)
@JoinColumn({name: "hub_id"})
unit: UnitEntity;
为了测试,我是这样安慰它的。
async findAllByUnit(unitId: number) {
const unit = await this.unitRepository.findOne(unitId)
console.log(unit.hub)
}
它returnsundefined
请让我知道我遗漏了什么。
提前致谢~
你必须包括这样的关系:
await this.unitRepository.findOne(unitId, {
relations: ['hub']
})