Skip to content

Commit

Permalink
fix(core): Fix default currencyCode change error in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 17, 2023
1 parent a0df530 commit 38e739a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export class ChannelService {
const [selectQbQuery, selectQbParams] = this.connection
.getRepository(ctx, ProductVariant)
.createQueryBuilder('variant')
.select('variant.id')
.select('variant.id', 'id')
.innerJoin(ProductVariantPrice, 'pvp', 'pvp.variantId = variant.id')
.andWhere('pvp.channelId = :channelId')
.andWhere('pvp.currencyCode = :newCurrencyCode')
Expand All @@ -347,13 +347,24 @@ export class ChannelService {
.update()
.where('channelId = :channelId')
.andWhere('currencyCode = :oldCurrencyCode')
.andWhere(`variantId NOT IN (${selectQbQuery})`, selectQbParams)
.set({ currencyCode: newCurrencyCode })
.setParameters({
channelId: channel.id,
oldCurrencyCode: originalDefaultCurrencyCode,
newCurrencyCode,
});

if (this.connection.rawConnection.options.type === 'mysql') {
// MySQL does not support sub-queries joining the table that is being updated,
// it will cause a "You can't specify target table 'product_variant_price' for update in FROM clause" error.
// This is a work-around from https://stackoverflow.com/a/9843719/772859
qb.andWhere(
`variantId NOT IN (SELECT id FROM (${selectQbQuery}) as temp)`,
selectQbParams,
);
} else {
qb.andWhere(`variantId NOT IN (${selectQbQuery})`, selectQbParams);
}
await qb.execute();
}
}
Expand Down

0 comments on commit 38e739a

Please sign in to comment.