gpt4 book ai didi

node.js - 使用原始查询时,如何在 Sequelize 中转换字段类型?

转载 作者:行者123 更新时间:2023-12-03 22:37:49 24 4
gpt4 key购买 nike

  • MSSQL 方言
  • 在表中我有一个 bigint 类型的列
  • 当我进行原始查询时,例如:
  • const product = {
    id: 1,
    isAvailable: true,
    lastUpdated: 4652082390 // epoch
    };

    await sequelize.query(`
    UPDATE Product SET
    isAvailable = $isAvailable,
    lastUpdated = $lastUpdated
    WHERE id = $id`,
    {
    bind: {
    location: latlong,
    isAvailable: isAvailable,
    lastUpdated: lastUpdated,
    id: product.id
    }
    }
    );

    它失败并出现错误:

    SequelizeDatabaseError: Validation failed for parameter 'lastUpdated'. Value must be between -2147483648 and 2147483647.



    看起来它将 lastUpdated 值视为整数,但不是 BIGINT :(

    我尝试了转换值类型,如文档所示: http://docs.sequelizejs.com/manual/raw-queries.html
    但看起来这个功能只适用于 Postgress 这不是我的情况

    最佳答案

    await sequelize.query(`
    UPDATE Product SET
    isAvailable = $isAvailable,
    lastUpdated = cast($lastUpdated as int)
    WHERE id = $id`,
    {
    bind: {
    location: latlong,
    isAvailable: isAvailable,
    lastUpdated: lastUpdated,
    id: product.id
    }
    }
    );

    关于node.js - 使用原始查询时,如何在 Sequelize 中转换字段类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145604/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com