代码之家  ›  专栏  ›  技术社区  ›  Ionică Bizău

表之间的续集和查询

  •  1
  • Ionică Bizău  · 技术社区  · 6 年前

    我有三种型号:

    Entity
      - id
      ...
    
    
    Event
      - id
      - metadata_field_id
      - entity_id
      - value (TEXT)
    
    MetadataField
      - id
      - name (TEXT)
    

    具有成对的数组 MetadataField.name s和 Event.value s、 我想构建一个查询,该查询将选择与这些对匹配的实体:应该是全局的 AND 然后对于每个事件, 事件价值 应为数组中的当前值,并且 Event.MetadataField.name 应为当前名称。

    我不知道如何使用Sequelize构建这个(我甚至不知道查询应该是什么样子)。

    我尝试的是:

    const eventValues = [["some_name", "some_value"], ["another_name", "another_value"]]
    if (eventValues.length) {                         
        queryObj.include = [{                         
            model: Event,             
            where: {
                [Op.and]: eventValues.map(c => ({     
                    value:  {                         
                        [Op.like]: `%${c[1]}%`        
                    }                                 
                }))                                   
            },                                        
            include: [{                               
                model: MetadataField, 
                [Op.and]: eventValues.map(c => ({     
                    name:  {                          
                        [Op.like]: c[0]               
                    }                                 
                }))                                   
            }]                                        
        }]                                            
    }                                                 
    return Entity.findAll(queryObj)   
    

    但这显然行不通,因为它想选择 MetadataField s名称具有 全部的 提供的名称和值相同。

    这应该如何实现?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Vivek Doshi    6 年前

    您只能在include模型内写入where条件并不是强制性的,但您也可以在该模型外写入where条件,但在这种情况下,您只需定义表名即可 '$table_name.field_name$' ,则,

    所以,给你:

    const eventValues = [["some_name", "some_value"], ["another_name", "another_value"]]
    if (eventValues.length) {                         
        queryObj.include = [{                         
            model: Event,                                        
            include: [{                               
                model: MetadataField
            }]                                        
        }],
        queryObj.where =   {
                [Op.or]: eventValues.map(c => ({     
                    '$events.value$' :  {  // <---- Magic is here                   
                        [Op.like]: `%${c[1]}%`        
                    } ,    
                    '$metadata_fields.name$' :  {  // <---- Magic is here                       
                        [Op.like]: c[0]               
                    }                            
                }))                                   
            }                                         
    }                                                 
    return Entity.findAll(queryObj)
    

    注意:您可能需要根据自己的更改表名,其中 已写入 // <---- Magic is here