【发布时间】: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