Transaction already closed: A batch query cannot be executed on a closed transavtion…
- User
Transaction already closed: A batch query cannot be executed on a closed transavtion error from prisma when importing large data with nested batch queries
- Assistant
This error occurs when Prisma tries to execute a batch query on a closed transaction. Batch queries are used to send multiple requests to the database in a single transaction, which can help improve performance and reduce the number of round trips to the database.
The error suggests that the transaction used to execute the batch query has already been closed, which means that the batch query can no longer be executed. This can happen if the transaction is closed manually, either by the code or by an external factor like a timeout.
One possible solution to this problem is to make sure that the transaction is not closed before the batch query is executed. You can also try to split the nested queries into smaller batches to reduce the load on the database and prevent timeouts. Additionally, you could try to increase the timeout limit if it is set too low.
- User
- Assistant
The error message suggests that the transaction being used to create the batch query has already been closed by the time the batch query is executed. A possible solution to this problem is to ensure that the transaction is open throughout the batch query execution.
You can achieve this by refactoring your code to create a nested transaction for each batch query. For example:
codeconst createMany = await prisma.$transaction(async (tx) => { const quotations = await Promise.all( body.quotations.map(async (quotation, index) => { ... return await tx.quotation.create({ ..., quotes: { createMany: { data: quotation.quotes!.map(async (quote) => { ... return await tx.quote.create({ ... }); }), }, }, ... }); }) ); ... }); - User
return await tx.quotation.create({ data: { questionnaireId: quotation.questionnaireId, collectorId: req.user!.id, itemCode: quotation!.itemCode, marketplaceCode: quotation.marketplaceCode!, quotes: { createMany: { data: quotation.quotes!.map((quote) => ({ ...quote, shopContactName: "Imported", shopContactPhone: "Imported", shopLatitude: "Imputated", shopLongitude: "Imputated", measurementUnit: items?.[index]!?.UoMCode, })), }, // quotation.quotes?.map((quote) => { return {...quote,quantity: items[index]?.measurementQuantity, // measurmentId: items[index]?.measurement,};}), }, status: QuotationStatus.BRANCH_APPROVED, creationStatus: QuotationCreationStatus.IMPORTED, }, select: { id: true, questionnaireId: true, itemCode: true, quotes: true, }, }); update this with the above solution
- Assistant
- User
return new TSError(diagnosticText, diagnosticCodes, diagnostics); ^ TSError: ⨯ Unable to compile TypeScript: src/Controllers/StatisticianController.ts:1236:19 - error TS2322: Type '{ price: number; quantity: number; shopContactName: string; shopContactPhone: string; shopLocation?: string | undefined; shopLatitude: string; shopLongitude: string; quotation: { connect: { ...; }; }; measurementUnit: string | undefined; }' is not assignable to type '(Without<QuoteCreateInput, QuoteUncheckedCreateInput> & QuoteUncheckedCreateInput) | (Without<...> & QuoteCreateInput)'. Type '{ price: number; quantity: number; shopContactName: string; shopContactPhone: string; shopLocation?: string | undefined; shopLatitude: string; shopLongitude: string; quotation: { connect: { ...; }; }; measurementUnit: string | undefined; }' is not assignable to type 'Without<QuoteUncheckedCreateInput, QuoteCreateInput> & QuoteCreateInput'. Type '{ price: number; quantity: number; shopContactName: string; shopContactPhone: string; shopLocation?: string | undefined; shopLatitude: string; shopLongitude: string; quotation: { connect: { ...; }; }; measurementUnit: string | undefined; }' is not assignable to type 'Without<QuoteUncheckedCreateInput, QuoteCreateInput>'. Types of property 'measurementUnit' are incompatible. Type 'string | undefined' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.
- Assistant
The first error indicates that the
measurementUnitfield of thedataobject passed to thetx.quote.createfunction is of the wrong type. The error message shows that themeasurementUnitproperty is expected to be of typeundefinedbut it is actually of typestring. To fix this error, you can change to .