【问题标题】:Mean Stack -> Sequelize Mysql AssociationMean Stack -> Sequelize Mysql 关联
【发布时间】:2016-04-24 04:28:03
【问题描述】:

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

'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;

};

我很确定表格创建正确

如果不存在则创建表 ProductCategoryMatches (id INTEGER NOT NULL auto_increment , total INTEGER, count INTEGER, ProductId INTEGER, createdAt DATETIME NOT NULL, updatedAt DATE KEY (id), FOREIGN KEY (ProductId) REFERENCES Products (id) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;

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

/** * 产品列表和类别 */ export.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
    });
});
};

【问题讨论】:

    标签: mysql angularjs sequelize.js mean-stack


    【解决方案1】:

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

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

    不确定这是否只是您发布的示例代码,但 Sequelize 关联的工作方式取决于每个模型。

    因此,ProductCategoryMatch 上与 Pr​​oducts 的关联将在 include 上起作用

    include: [{ model: db.Product }]
    

    但您必须明确地将关联添加到 Product 以使包含以另一种方式工作:

    include: [{ model: db.ProductCategoryMatch }]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2022-01-22
      • 2016-06-19
      相关资源
      最近更新 更多