Sequelize 模型重复值检查

Posted

技术标签:

【中文标题】Sequelize 模型重复值检查【英文标题】:Sequelize Model Duplicate value check 【发布时间】: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。

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

    如果存在具有相同 iata 代码且 isdeleted = 0 的承运人 如果存在具有相同 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 的实例以便处理它并更轻松地显示它?

【问题讨论】:

【参考方案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!

【讨论】:

以上是关于Sequelize 模型重复值检查的主要内容,如果未能解决你的问题,请参考以下文章

如何在 sequelize 查询中使用查询参数作为列名

node开发

建立Model

sequelize.query() 两次返回相同的结果

是否可以使用 Graphql 和 Sequelize 进行多租户?

使用 sequelize 通过相同两个表之间的单独联结表进行查询