NestJS - TypeORM - ManyToOne 实体未定义

NestJS - TypeORM - ManyToOne Entity is undefined

我正在尝试 link 实体 Child & Client

所以我创建了 2 个实体:

// src/children/child.entity.ts

@Entity()
export class Child extends BaseEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstname: string;

  @Column()
  lastname: string;

  @ManyToOne(type => Client, client => client.children, { eager: false })
  client: Client

  @Column()
  clientId: number;
}

// src/clients/client.entity.ts

@Entity()
export class Client extends BaseEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstname: string;

  @Column()
  lastname: string;

  @OneToMany(type => Child, child => child.client, { eager: true })
  children: Child[];

}

我可以创建一个 client 和一个 child。当我得到一个 client 时,我可以检索他所有的 children.

但我无法用 child 检索 client :

例如,在children.controller.ts

当我访问http://locahost:3000/children/1/parent

我进入了控制台:

我真的卡住了,因为我不知道如何解决这个问题。

这里是children.module.ts

import { Module } from '@nestjs/common';
import { ChildrenController } from './children.controller';
import { ChildrenService } from './children.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ChildRepository } from './child.repository';
import { ClientRepository } from 'src/clients/client.repository';
import { ClientsService } from 'src/clients/clients.service';
import { Client } from 'src/clients/client.entity';

@Module({
  imports: [
    TypeOrmModule.forFeature([ChildRepository, ClientRepository, Client]),
  ],
  controllers: [ChildrenController],
  providers: [ChildrenService, ClientsService]
})
export class ChildrenModule {}

这里是typeorm.config

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export const typeOrmConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'myusername',
  password: '',
  database: 'mydatabase',
  entities: [__dirname + '/../**/*.entity.{js,ts}'],
  synchronize: true,
}

编辑 1 :

children.controller.ts

// src/children/children.controller.ts

@Controller('children')
export class ChildrenController {

  constructor(
    private childrenService: ChildrenService,
    private clientsService: ClientsService
  ) {}

  @Get('/:id')
  async getChild(id: number): Promise<Child> {
    return await this.childrenService.getChild(id);
  }
}

children.service.ts

// src/children/children.service.ts

@Injectable()
export class ChildrenService {
  constructor(
    @InjectRepository(ChildRepository)
    private childRepository: ChildRepository,
    @InjectRepository(ClientRepository)
    private clientRepository: ClientRepository
  ) {}

  async getChild(id: number): Promise<Child> {
    return await this.childRepository.findOne(id)
  }
}

谢谢你的帮助

在我看来,您忘记了在此关系的 ManyToOne 端添加 @JoinColumn()。尝试以这种方式增强您的代码


@Entity()
export class Child extends BaseEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstname: string;

  @Column()
  lastname: string;

  @ManyToOne(type => Client, client => client.children, { eager: false })
  @JoinColumn()
  client: Client

  @Column()
  clientId: number;
}

此外,请注意 eager 参数。如果它设置为 true 这个关系将被自动加载,即使你没有手动要求它,否则你必须指定你想要加载的关系。 尝试像这样增强 children.service.ts

  async getChild(id: number): Promise<Child> {
    return await this.childRepository.findOne(id, {
      relations: ['client']
    })
  }

或者你可以设置这个关系为eager,每次加载时都会自动加载Child

  @ManyToOne(type => Client, client => client.children, { eager: true })
  @JoinColumn()
  client: Client

如@Yevhenii 所述,您忘记添加 JoinColumn 但必须指定客户列:

    @Entity()
export class Child extends BaseEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstname: string;

  @Column()
  lastname: string;

  @ManyToOne(()=> Client, (client) => client.children, { eager: false })
  @JoinColumn([{ name: "clientId", referencedColumnName: "id" }])
  client: Client

  @Column()
  clientId: number;
}

不要犹豫,告诉我结果。