typescript 设置默认值,而不是保存实体时传递的值

dbf7pr2w  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(109)

我正在使用NestJS/TypeORM,我需要在实体中播种数据。
我有一个表A链接到B和C与ManyToOne关系。一个B或C链接到多个A。
我使用DataFactory生成种子,然后在A条目中添加2个字段:idB和idC

const A = DataFactory.createForClass(A).generate(qty);
for (let i = 0; i < A.length; i++) {
      const randB= randomFromArray(B);
      A[i].idB= randB.idB;
      const randC= randomFromArray(C);
      A[i].idC= randC.idC;
    }

它对C非常有效,但对于B,A表中的列idB总是NULL,尽管发送了正确的数据。
我检查了MySQL日志,似乎MySQL没有接收到idB,因为它传递了DEFAULT。对于idC,它传递正确的数据。
我很迷茫,我检查了我的A,B和C实体,我找不到一个错误,甚至在关系/模型等小的差异。
完整的seeder代码:

import { A } from '@Entity/A.entity';
import { BsRepository } from '@Repositories/Bs.repository';
import { CsRepository } from '@Repositories/Cs.repository';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DataFactory } from 'nestjs-seeder';
import { Repository } from 'typeorm';
import { randomFromArray } from 'utils/fonctions';
import { CSeeder } from './Cs.seeder';
import { NestFactory } from '@nestjs/core';
import { AppModule } from 'src/app.module';
import { BsSeeder } from './Bs.seeder';

@Injectable()
export class ASeeder {
  constructor(
    @InjectRepository(A)
    private readonly ARepository: Repository<A>,
    private readonly CRepository: CsRepository,
    private readonly BRepository: BsRepository
  ) {}

  async seed(qty): Promise<any> {
    // Génération des A
    const As =
      DataFactory.createForClass(A).generate(qty);

    // Récupération de tous les Bs et lien aléatoire avec les A
    let Bs = await this.BsRepository.findAll();
    if (Bs.length === 0) {
      const app = await NestFactory.create(AppModule);
      const seeder = app.get(BsSeeder);
      await seeder.seed(10);
      await app.close();
    }

//Added this line
    Bs = await this.BsRepository.findAll();

    // Vérification qu'il existe au moins 1 C dans la BDD, sinon création d'un C
    let Cs = await this.CsRepository.findAll();

    if (Cs.length === 0) {
      const app = await NestFactory.create(AppModule);
      const seeder = app.get(CSeeder);
      await seeder.seed();
      await app.close();
    }

    Cs = await this.CsRepository.findAll();
    console.log('Cs', randomFromArray(Cs));

    for (let i = 0; i < As.length; i++) {
      const randB = randomFromArray(Bs);
      As[i].idBA = randB.idB;
      const randC = randomFromArray(Cs);
      As[i].idCA = randC.idC;
    }

    console.log('As', As);
    await this.ARepository.save(As);
  }

  async drop(): Promise<any> {
    return this.AsRepository.delete({});
  }
}
vbkedwbf

vbkedwbf1#

我相信这是因为,不像C,你不会在你的“空支票”后第二次读B。请参见代码中的注解。

//...
@Injectable()
export class ASeeder {
  constructor(
    @InjectRepository(A)
    private readonly ARepository: Repository<A>,
    private readonly CRepository: CsRepository,
    private readonly BRepository: BsRepository
  ) {}

  async seed(qty): Promise<any> {

    const As =
      DataFactory.createForClass(A).generate(qty);

    //read Bs a first time
    const Bs = await this.BsRepository.findAll();

    //if Bs empty, add value to its repo
    if (Bs.length === 0) {
      const app = await NestFactory.create(AppModule);
      const seeder = app.get(BsSeeder);
      await seeder.seed(10);
      await app.close();
    }

    //never reads Bs again

    //reads Cs a first time
    let Cs = await this.CsRepository.findAll();

    //if Cs empty, add value to its repo
    if (Cs.length === 0) {
      const app = await NestFactory.create(AppModule);
      const seeder = app.get(CSeeder);
      await seeder.seed();
      await app.close();
    }

    //reads Cs a second time after its populated
    Cs = await this.CsRepository.findAll();
    console.log('Cs', randomFromArray(Cs));

    for (let i = 0; i < As.length; i++) {
      const randB = randomFromArray(Bs);
      As[i].idBA = randB.idB;
      const randC = randomFromArray(Cs);
      As[i].idCA = randC.idC;
    }

    console.log('As', As);
    await this.ARepository.save(As);
  }

  async drop(): Promise<any> {
    return this.AsRepository.delete({});
  }
}

相关问题