What is the correct way how to create relation in typeorm?

前端 未结 2 1573
小蘑菇
小蘑菇 2021-01-23 10:00

I have two entities:

@Entity({ name: \'provider\' })
export class ProviderEntity extends GenericEntity {

    @Column()
    name: string;

    @Column()
    descr         


        
2条回答
  •  温柔的废话
    2021-01-23 10:28

    I believe to associate things by a relation ID, and not a full relation object, you need to add the relation ID to your interface:

    @ManyToOne(() => ProviderEntity, provider => provider.items)
    provider: Promise;
    
    @Column()
    providerId: string
    

    providerId is the column TypeORM uses to keep track of the relation internally, you simply simply need to expose it publicly.

    And then you simply set that property:

    const item = new ItemEntity();
    item.content = content;
    item.providerId = providerId; // set providerId column directly.
    
    await this.repository.save(item);
    

提交回复
热议问题