【问题标题】:Sequelize - How to Insert data, and then updateSequelize - 如何插入数据,然后更新
【发布时间】:2021-09-27 21:19:42
【问题描述】:

我正在为 node.js/maria db 项目使用 Sequalize ORM。

我想做的是,使用原始查询为产品数据生成新表。

我的逻辑顺序写在下面。

步骤 1. 销毁表以重置数据。

第 2 步。插入产品数据。

第 3 步。更新产品数据中的价格数据。

第 4 步。更新产品数据中的库存数据。

问题是第 3 步和第 4 步。它不起作用! 我发现...“插入”需要一些时间才能完成。因此,“更新”无法完成,因为还没有产品数据。

在步骤 2 完成后不久调用步骤 3~4 有什么想法吗?

提前致谢。

const generateProductList = () => {

    return new Promise( async (resolve, reject) => {

        try {

            await ProductPresentation.destroy({ truncate: true })

            const productSql = `INSERT INTO m_product_presentation (productId, sku, name, status, urlKey, category, shortDescription, imageSmall, imageThumbnail) 
            SELECT id, sku, name, status, urlKey, category, shortDescription, imageSmall, imageThumbnail FROM m_product;`

            const priceSql = `UPDATE m_product_presentation INNER JOIN m_price
            ON m_product_presentation.productId = m_price.productId
            SET m_product_presentation.priceRrp = m_price.priceRrp, m_product_presentation.priceRegular = m_price.priceRegular, m_product_presentation.priceSpecial = m_price.priceSpecial;`

            const stockSql = `UPDATE m_product_presentation INNER JOIN m_inventory
            ON m_product_presentation.productId = m_inventory.productId
            SET m_product_presentation.stockAvailability = m_inventory.stockAvailability, m_product_presentation.stockQty = m_inventory.stockQty;`


            // What I want is, Create initial data first. And then, update price and stock info. But, It fail...
            await ProductPresentation.sequelize.query(productSql, { type: QueryTypes.INSERT })
            await ProductPresentation.sequelize.query(priceSql, { type: QueryTypes.UPDATE })
            await ProductPresentation.sequelize.query(stockSql, { type: QueryTypes.UPDATE })

            resolve()

        } catch(err) {

            reject(err)
            logger.error(err)

        }

    })
}

【问题讨论】:

  • 为什么不第一时间插入更新库存和更新价格?为什么要在同一个处理程序中多次运行与同一产品相关的查询?

标签: node.js orm mariadb sequelize.js


【解决方案1】:

您可以做的是,在更新依赖于同一产品的值之前,您可以检查该产品是否已经输入。

const generateProductList = () => {
  return new Promise(async (resolve, reject) => {
    try {
      await ProductPresentation.destroy({ truncate: true })
      const productSql = `INSERT INTO m_product_presentation (productId, sku, name, status, urlKey, category, shortDescription, imageSmall, imageThumbnail) 
            SELECT id, sku, name, status, urlKey, category, shortDescription, imageSmall, imageThumbnail FROM m_product;`
      const priceSql = `UPDATE m_product_presentation INNER JOIN m_price
            ON m_product_presentation.productId = m_price.productId
            SET m_product_presentation.priceRrp = m_price.priceRrp, m_product_presentation.priceRegular = m_price.priceRegular, m_product_presentation.priceSpecial = m_price.priceSpecial;`
      const stockSql = `UPDATE m_product_presentation INNER JOIN m_inventory
            ON m_product_presentation.productId = m_inventory.productId
            SET m_product_presentation.stockAvailability = m_inventory.stockAvailability, m_product_presentation.stockQty = m_inventory.stockQty;`

      // Inserting the product in the table
      const product = await ProductPresentation.sequelize.query(productSql, { type: QueryTypes.INSERT })
      // To find if the product is existing the table or not
      const isProductEntered = await sequelize.query(`
      select exists(select 1 from "m_product_presentation" where "productId"=$1)
      `, {
        bind: [`${product[0][0].productId}`],
        type: QueryTypes.SELECT
      });
      if (isProductEntered[0].exists) {
        await ProductPresentation.sequelize.query(priceSql, { type: QueryTypes.UPDATE })
        await ProductPresentation.sequelize.query(stockSql, { type: QueryTypes.UPDATE })
      }
      
      resolve()

    } catch (err) {
      reject(err)
      logger.error(err)
    }
  })
}

如果仅输入产品,则将执行更新查询。

【讨论】:

  • 感谢您的友好指导。但是..我发现根本原因是另一个源代码。所以我成功地修复了它。 :)
【解决方案2】:

我发现另一个源代码(前端的一些反应代码)导致了这个问题。我会关闭这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2021-11-23
    • 2020-04-30
    • 2023-03-17
    相关资源
    最近更新 更多