这是我致电product.getAttributeValues()时遇到的错误:
未处理的拒绝SequelizeDatabaseError:未知列 “字段列表”中的“ ProductAttribute.attribute_id”
我不知道ProductAttribute.attribute_id来自何处
这是产生以下内容的SQL查询:
执行(默认):SELECT
AttributeValue
。attribute_value_id
,AttributeValue
。value
,AttributeValue
。attribute_id
,ProductAttribute
。product_id
如ProductAttribute.product_id
,ProductAttribute
。attribute_id
如ProductAttribute.attribute_id
,ProductAttribute
。attribute_value_id
为ProductAttribute.attribute_value_id
,ProductAttribute
。attribubte_value_id
为ProductAttribute.attribubte_value_id
来自attribute_value
ASAttributeValue
作为product_attribute
的内部联接ProductAttribute
开启AttributeValue
。attribute_value_id
=ProductAttribute
。attribubte_value_id
并且ProductAttribute
。product_id
= 1;
我有这个模式:
这些Sequelize模型关联:
module.exports = (sequelize, DataTypes) => {
var Product = sequelize.define('Product', {
product_id: {
autoIncrement: true,
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
},
}, {tableName: 'product', timestamps: false},
);
Product.associate = function(models) {
Product.belongsToMany(models.AttributeValue, { through: 'ProductAttribute', foreignKey: 'product_id', otherKey: 'attribubte_value_id'})
Product.belongsTo(models.Category, { through: 'ProductCategory', foreignKey: 'product_id', otherKey: 'category_id'})
};
return Product;
};
module.exports = (sequelize, DataTypes) => {
var AttributeValue = sequelize.define('AttributeValue', {
attribute_value_id: {
autoIncrement: true,
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
},
value: {
type: DataTypes.STRING,
allowNull: false,
},
attribute_id: {
allowNull: false,
type: DataTypes.INTEGER,
references: {
// This is a reference to another model
model: models.Attribute,
// This is the column name of the referenced model
key: 'attribute_id',
}
},
}, {tableName: 'attribute_value', timestamps: false},
);
AttributeValue.associate = function(models) {
AttributeValue.hasOne(models.Attribute, {foreignKey: 'attribute_id'})
AttributeValue.belongsToMany(models.Product, {through: 'ProductAttribute', foreignKey: 'attribute_value_id', otherKey: 'product_id'})
};
return AttributeValue;
};
module.exports = (sequelize, DataTypes) => {
var ProductAttribute = sequelize.define('ProductAttribute', {
product_id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
attribute_id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
}, {tableName: 'product_attribute', timestamps: false},
);
ProductAttribute.associate = function(models) {
};
return ProductAttribute;
};
如何设置关联,以便product.getAttributeValues()返回某个产品的所有attributeValue,反之亦然