Witam.
Mam dwie tabele trzymajace dane osób Agent, Customer. Każdy agent i każdy Customer może mieć jeden lub więcej adresów pocztowych. Wprowadziłem więc osobną tabelę z adresami. Wszystkie tabele są oparte o klucz główny id: number. Oprócz klucza id wprowadziłem do tabel agent i customer kolumnę uuid i oczywiście w tabeli addresss kolumnę owneruuid jako klucz obcy. No i teraz pytanie jak to zamodelować w TypeORM jako relacje OneToMany z tabel Agent i Customer do tabeli Address. Moje klasy (na potrzeby posta mocno okrojone) wyglądaja następująco:
export class EntityBase {
@ApiProperty()
@PrimaryGeneratedColumn()
id: number;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}
@Entity('agents')
export class Agent extends EntityBase {
@Column({ unique: true })
@Generated('uuid')
uuid: string;
@OneToMany(() => Address, (address) => address.ownerid)
@JoinColumn({ name: 'uuid', referencedColumnName: 'owneruuid' })
addresses: Address[];
}
@Entity('customer')
export class Customer extends EntityBase {
@Column({ unique: true })
@Generated('uuid')
uuid: string;
@OneToMany(() => Address, (address) => address.owneruuid)
@JoinColumn({ name: 'uuid', referencedColumnName: 'owneruuid' })
addresses: Address[];
}
@Entity('addresses')
export class Address extends EntityBase {
@Column({ type: 'uuid', length: 36 })
owneruuid: string;
}
Oczywiście powyższa konfiguracja @OneToMany() i @JoinColumn() nie działa. Da się to zamodelować w TypeORM, czy jestem skazany na ręczne budowanie osobnych zapytań w QueryBuilderze? Jakieś inne pomysły?