Sequelize: принадлежит ассоциации ToMany через таблицу соединений - PullRequest
0 голосов
/ 04 марта 2019

Это ошибка, которую я получаю при вызове product.getAttributeValues ​​():

Необработанный отказ SequelizeDatabaseError: Неизвестный столбец 'ProductAttribute.attribute_id' в 'списке полей'

Я не знаю, откуда берется ProductAttribute.attribute_id

. Это SQL-запрос, который выдает:

Выполнение (по умолчанию): SELECT AttributeValue. attribute_value_id, AttributeValue. value, AttributeValue. attribute_id, ProductAttribute. product_id AS ProductAttribute.product_id, ProductAttribute. attribute_id AS ProductAttribute.attribute_id, ProductAttribute. attribute_value_id ASProductAttribute.attribute_value_id, ProductAttribute. attribubte_value_id AS ProductAttribute.attribubte_value_id ОТ attribute_value КАК AttributeValue ВНУТРЕННЕЕ СОЕДИНЕНИЕ product_attribute КАК ProductAttribute ВКЛ AttributeValue. attribute_value_id = ProductAttribute. attribubte_value_id И ProductAttribute. product_id = 1;

У меня есть эта схема:

enter image description here

И эти ассоциации моделей 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 определенного продукта и наоборот

...