【问题标题】:Sequelize Model Duplicate value checkSequelize 模型重复值检查
【发布时间】:2018-11-21 14:24:53
【问题描述】:

我是 sequelize 和 ORM 的新手,我正在尝试创建一个模型。这就是我所拥有的:

/* Carrier model definition */
const carrier = sequelize.define('carrier', {
    /* The unique carrier id */
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
    },
    /* The iata code of the carrier */
    iata: {
        type: Sequelize.STRING,
        validate: {
            is: {
                args: ["^[A-Z]{1}[0-9]{1}$|^[A-Z]{1}[A-Z]{1}$|^[0-9]{1}[A-Z]{1}$|^\s*$"],
                msg: "Carrier iata code is invalid."
            }
        }
    },
    /* The icao code of the carrier */
    icao: {
        type: Sequelize.STRING,
        validate: {
            is: {
                args: ["^[A-Z]{3}$|^\s*$"],
                msg: "Carrier icao code is invalid."
            }
        }
    },
    /* The deleted flag of the carrier */
    isdeleted: {
        type: Sequelize.INTEGER,
        validate: {
            isIn: {
                args: [
                    [0, 1]
                ],
                msg: "Carrier \"is deleted value\" can be 0 or 1."
            }
        }
    }
}, {
    /* Perform model validations */
    validate: {
        carrierValidator() {

            /* Ensure that at least an iata or an icao code exists */
            if ((this.iata == "") && (this.icao == "")) {

                throw new Error('Carrier must have at least an iata code or an icao code or both.');

            }

        }
    },
    /* Apply hooks */
    hooks: {
        beforeValidate: (carrier, options) => {

            /* Capitalize carrier iata code */
            carrier.iata = (typeof carrier.iata === 'undefined') ? "" : carrier.iata.toUpperCase();

            /* Capitalize carrier icao code */
            carrier.icao = (typeof carrier.icao === 'undefined') ? "" : carrier.icao.toUpperCase();

            /* Set default isdeleted value to 0 */
            carrier.isdeleted = (typeof carrier.isdeleted === 'undefined') ? 0 : parseInt(carrier.isdeleted);

        }
    }

})

isdeleted 属性是一个定义条目是否被删除的列。如果它被删除,它的值是 1,如果不是 0。

现在,如果以下情况之一为真,我想阻止插入新的运营商:

  1. 如果存在具有相同 iata 代码且 isdeleted = 0 的承运人
  2. 如果存在具有相同 icao 代码且 isdeleted = 0 的承运人

我设法通过在 validate 中添加以下块来实现它(对于 iata 案例):

if (this.iata != ""){

    sequelize.models.carrier.findAndCountAll({
      where: {
        iata: this.iata,
        isdeleted:0
      }
    }).then((result) => {

        if (result.rows != 0){

            throw new Error("Carrier iata code already exists.");

        }

  })

}

当我尝试插入一个副本时,我得到了一个非常难看的响应:

有没有办法获取ValidationError 的实例以便处理它并更轻松地显示它?

【问题讨论】:

    标签: error-handling orm sqlite sequelize.js


    【解决方案1】:

    好吧,看来我有点累了,我错过了返回......

    if (this.iata != ""){
    
    return sequelize.models.carrier.findAndCountAll({
      where: {
        iata: this.iata,
        isdeleted:0
      }
    }).then((result) => {
    
        if (result.rows != 0){
    
            throw new Error("Carrier iata code already exists.");
    
            }
    
      })
    
    }
    

    感谢 sequelize slack 的 florin!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-30
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多