我有两个相关的模型,Catalog和ProductCategory.后者有一个组合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'});
Run Code Online (Sandbox Code Playgroud)
我正在尝试包含与Catalog相关的ProductCategory表中的一些信息,但仅限于language_id匹配时.
目前我正在从两张桌子上得到所有可能的比赛.这是现在的查询:
Catalog.find({where:
{id: itemId},
include: {
model: models.ProductCategory,
where: {language_id: /* Catalog.language_id */}
}
}) …Run Code Online (Sandbox Code Playgroud)