平均堆栈 - > Sequelize Mysql Association

时间:2016-01-18 21:17:35

标签: mysql angularjs sequelize.js mean-stack

我正在尝试在两个表之间创建关联。我收到一个表没有关联的错误。

'use strict';

module.exports = function(sequelize, DataTypes) {

var Product = sequelize.define('Product', {
        name:               DataTypes.TEXT,
        description:        DataTypes.TEXT,
        manufacturer:       DataTypes.CHAR(150),
        stockCode:          DataTypes.CHAR(150),
        price:              DataTypes.FLOAT,
        weight:             DataTypes.FLOAT,
        stockLevel:         DataTypes.FLOAT,
        imageUrl:           DataTypes.STRING,
        category:           DataTypes.CHAR(50),
        pathId:             DataTypes.INTEGER(11),
        sku:                DataTypes.CHAR(64),
        upc:                DataTypes.CHAR(64),
        ean:                DataTypes.CHAR(64),
        jan:                DataTypes.CHAR(64),
        isbn:               DataTypes.CHAR(64),
        stockStatusId:      DataTypes.INTEGER(11),
        imageDest:          DataTypes.TEXT,
        image:              DataTypes.TEXT,
        points:             DataTypes.INTEGER(8),
        shipping:           DataTypes.INTEGER(1),
        dateAvaliable:      DataTypes.DATE,
        weightClassId:      DataTypes.INTEGER(11),
        lengthClassId:      DataTypes.INTEGER(11),
        length:             DataTypes.DECIMAL(15,8),
        width:              DataTypes.DECIMAL(15,8),
        height:             DataTypes.DECIMAL(15,8),
        subtract:           DataTypes.INTEGER(1),
        minimum:            DataTypes.INTEGER(11),
        sortOrder:          DataTypes.INTEGER(11),
        tag:                DataTypes.TEXT,
        metaTitle:          DataTypes.CHAR(255),
        metaDescription:    DataTypes.CHAR(255),
        metaKeyword:        DataTypes.CHAR(255),
        discontinued:       {
            type            :DataTypes.BOOLEAN,
            defaultValue    : 0
        },
    },
    {
        associate: function(models){
            Product.belongsTo(models.Supplier);
            //Product.hasMany(models.ProductCategoryMatch);
        }
    }
);

return Product;

};

'use strict';

module.exports = function(sequelize, DataTypes) {

var ProductCategoryMatch = sequelize.define('ProductCategoryMatch', {
        total:              DataTypes.INTEGER,
        count:              DataTypes.INTEGER,
        productId:          DataTypes.INTEGER,
        // categoryId:          DataTypes.INTEGER,
    },
    {
        associate: function(models){
             ProductCategoryMatch.belongsTo(models.Product, {foreignkey : 'ProductId'});
            // ProductCategoryMatch.belongsTo(models.Category, {foreignkey : 'categoryId'});
        }
    }
);

return ProductCategoryMatch;

};

我很确定该表正在正确创建

CREATE TABLE IF NOT NOT EXISTS ProductCategoryMatchesid INTEGER NOT NULL auto_increment,total INTEGER,count INTEGER,ProductId INTEGER,createdAt DATETIME NOT NULL,updatedAt DATETIME NOT NULL,PRIMARY KEY(id),FOREIGN KEY(ProductId)REFERENCES Productsid)ON DELETE NO ACTION ON UPDATE CASCADE)ENGINE = InnoDB;

然后,当我试图找到我得到一个错误 错误:ProductCategoryMatch与Product!

无关

/ **  *产品清单和类别  * / exports.categories = function(req,res){

db.Product.findAll({
    attributes : [
        'name',
        // 'ProductCategoryMatch.count'
    ],
    include : [
        { model: db.ProductCategoryMatch }
    ],
    // order : [

    // ]
}).then(function(products){
    return res.jsonp(products);
}).catch(function(err){
    return res.render(500, {
        error: err,
        status: 500
    });
});
};

1 个答案:

答案 0 :(得分:0)

在上面发布的代码中,从Products到ProductCategoryMatch的关联被注释掉了:

associate: function(models){
  Product.belongsTo(models.Supplier);
  //Product.hasMany(models.ProductCategoryMatch);
}

不确定这只是您发布的示例代码,但Sequelize协会的工作方式是基于每个模型。

因此,ProductCategoryMatch上与Products的关联将适用于include

include: [{ model: db.Product }]

但您必须明确地将关联添加到Product以使include以其他方式工作:

include: [{ model: db.ProductCategoryMatch }]