[英]NestJS - TypeORM - ManyToOne Entity is undefined
我正在尝试链接到实体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;
}
不要犹豫,告诉我结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.