NestJS - TypeORM - ManyToOne 实体未定义

rca*_*tte 6 entity many-to-one typescript typeorm nestjs

我正在尝试链接到实体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;
}
Run Code Online (Sandbox Code Playgroud)

// 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[];

}
Run Code Online (Sandbox Code Playgroud)

我能够创建 a client& a child。当我得到一个时client,我就可以找回他的一切children

但我无法用 aclient检索child

例如,在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 {}

Run Code Online (Sandbox Code Playgroud)

这是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,
}
Run Code Online (Sandbox Code Playgroud)

编辑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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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)
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Yev*_*nii 10

在我看来,您似乎忘记了@JoinColumn()在这种关系的 ManyToOne 方面添加。尝试用这种方式增强你的代码


@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;
}
Run Code Online (Sandbox Code Playgroud)

另外,还要注意eager参数。如果设置为该true关系,即使您不需要手动加载,也会自动加载,否则您必须指定要加载的关系。尝试children.service.ts像这样增强:

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

或者您可以将此关系设置为eager每次加载时都会自动加载Child

  @ManyToOne(type => Client, client => client.children, { eager: true })
  @JoinColumn()
  client: Client
Run Code Online (Sandbox Code Playgroud)