Javascript Sequelize:Include.where 按“父”模型属性过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30052254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:34:39  来源:igfitidea点击:

Sequelize: Include.where filtering by a 'parent' Model attribute

javascriptsequelize.js

提问by Sandokan El Cojo

I have two Models related, Catalog and ProductCategory. The latter has a composed PK, 'id, language_id'. Here are the models simplified:

我有两个相关的模型,目录和产品类别。后者有一个组合 PK,'id,language_id'。以下是简化模型:

var Catalog = sequelize.define("Catalog", {
id: {
  type: DataTypes.INTEGER,
  primaryKey: true,
  autoIncrement: true
},
user_id: {
  type: DataTypes.INTEGER,
  allowNull: false
},
product_category_id: {
  type: DataTypes.STRING(7)
},
language_id: {
  type: DataTypes.INTEGER
},  
... more stuff ...
}

var ProductCategory = sequelize.define("ProductCategory", {
id: {
  type: DataTypes.STRING(7),
  primaryKey: true
},
language_id: {
  type: DataTypes.INTEGER,
  primaryKey: true
},
... more stuff ...
}

Catalog.belongsTo(models.ProductCategory, {foreignKey: 'product_category_id'});

I'm trying to include some info from ProductCategory table related to Catalog, but ONLY when the language_id matches.

我试图从与目录相关的 ProductCategory 表中包含一些信息,但仅当 language_id 匹配时。

At the moment I'm getting all the possible matches from both tables. This is the query right now:

目前我正在从两个表中获取所有可能的匹配项。这是现在的查询:

Catalog.find({where:
    {id: itemId},
    include: {
        model: models.ProductCategory, 
        where: {language_id: /* Catalog.language_id */}
    }
})

Is there a way to use an attribute from Catalog to filter the include where both models have the same language?

有没有办法使用目录中的属性来过滤两个模型具有相同语言的包含?

By the way, I've also tried changing the where caluse, without any consecuence:

顺便说一下,我也试过改变 where caluse,没有任何后果:

where: {'ProductCategory.language_id': 'Catalog.language_id'}

回答by Frederik Kammer

Sequelize provides an extra operator $colfor this case so you don't have to use sequelize.literal('...')(which is more a hack).

Sequelize$col为这种情况提供了一个额外的运算符,因此您不必使用sequelize.literal('...')(这更像是一种黑客行为)。

In your example the usage would look like this:

在您的示例中,用法如下所示:

Catalog.find({where:
    {id: itemId},
    include: {
        model: models.ProductCategory, 
        where: {
          language_id: {$col: 'Catalog.language_id'}
        }
    }
})

回答by itsHarshad

You can try this (Especially if you are using MariaDB) -

你可以试试这个(特别是如果你使用的是 MariaDB) -

const Sequelize = require('sequelize'); 
const op = Sequelize.Op;

Catalog.find({where:
    {id: itemId},
    include: {
        model: models.ProductCategory, 
        where: {
          language_id: {[op.col]: 'Catalog.language_id'}
        }
    }
})

回答by Sandokan El Cojo

This seems this do the trick:

这似乎可以解决问题:

where: {language_id: models.sequelize.literal('Catalog.language_id')}