Search code examples
mysqltypescriptnestjstypeorm

Insert into select using typeom


I try to execute an "INSERT INTO SELECT" using typeorm in a nestjs project. I've correctly sett the subscriber part, but when I want to execute my function, I have an error

(node:4592) UnhandledPromiseRejectionWarning: TypeError: statement.replace is not a function (node:4592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

Here my function

async insertIntoSelect(id) {
    const qb = this.MyOperationRepository.createQueryBuilder();
    return await qb
      .createQueryBuilder()
      .insert()
      .into(EntityA, ['FieldA', 'FieldB'])
      .select(id, 'FieldB')
      .from(EntityB, 'EntityB')
      .where('EntityB.id= ' + id)
      .printSql()
      .execute();
  }

I don't know if it's my function or the process which is not corrrect. If someone have an exemple or see something wrong in my code, please tell me.


Solution

  • Seems like INSERT INTO ... SELECT is not currently supported in TypeOrm. You either need use a raw query, use a subquery, or perform multiple queries.

    More possible solutions here.