Node JS Sequelize Data Modeling with Custom Reference字段

时间:2015-01-27 10:55:45

标签: javascript node.js sequelize.js

我定义了以下模型:

module.exports = function(sequelize, DataTypes) {

// Brand
var Brand = sequelize.define("Brand", {
   name: DataTypes.STRING
},{tableName: "brand"});

// Product_Type
var Product_Type = sequelize.define("Product_Type", {
   name: DataTypes.STRING
},{tableName: "product_type"});

// Product
var Product = sequelize.define("Product", {
  name: DataTypes.STRING,
  product_type_id: {
    type: DataTypes.INTEGER,
    references: Product_Type,
    referencesKey: "id"
},
brand_id: {
    type: DataTypes.INTEGER,
    references: Brand,
    referencesKey: "id"
},
 weight: DataTypes.INTEGER
},{tableName: "product"});

Brand.hasMany(Product);
Product_Type.hasMany(Product);

Product.belongsTo(Brand);
Product.belongsTo(Product_Type);

return Product;
};

我收到此错误:

 Possibly unhandled SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'BrandId' in 'field list'

如何判断sequelize使用正确的字段?使用BrandId代替brand_id而不是ProductTypeId来使用product_type_id。 我认为引用它就足够了,还是我通过关联再次指定它?

0 个答案:

没有答案