вот миграции для понимания сути.
Категории
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('categories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
image: {
type: Sequelize.STRING
},
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('categories');
}
};
Товар
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('goods', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
cost: {
type: Sequelize.INTEGER
},
image: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('goods');
}
};
связанная таблица товар к категории
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('goods_to_categories', {
goods_id: {
type: Sequelize.INTEGER,
references: {
model: {
tableName: 'goods',
key:'id'
},
onDelete: 'cascade',
onUpdate: 'restrict',
},
},
category_id: {
type: Sequelize.INTEGER,
references: {
model: {
tableName: 'categories',
key:'id'
},
onDelete: 'cascade',
onUpdate: 'restrict',
},
},
}).then(()=>queryInterface.addIndex('goods_to_categories',['goods_id','category_id']));
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('goods_to_categories');
}
};
вот модели
category
'use strict';
module.exports = (sequelize, DataTypes) => {
const category = sequelize.define('category', {
name: DataTypes.STRING,
description: DataTypes.STRING,
image: DataTypes.STRING,
}, {});
category.associate = function(models) {
category.belongsToMany(models.goods, { through: 'goods_to_category',as: 'goods',foreignKey: 'goods_id'});
};
return category;
};
goods
'use strict';
const Category = require("./category");
module.exports = (sequelize, DataTypes) => {
const goods = sequelize.define('goods', {
name: DataTypes.STRING,
description: DataTypes.STRING,
cost: DataTypes.INTEGER,
image: DataTypes.STRING,
}, {});
goods.associate = (models)=> {
console.log(models);
goods.belongsToMany(models.category, {
through: 'goods_to_category',
foreignKey: 'category_id',
as: 'category'
});
};
return goods;
};
goods_to_category
'use strict';
module.exports = (sequelize, DataTypes) => {
const goods_to_category = sequelize.define('goods_to_category', {
goods_id: DataTypes.INTEGER,
category_id: DataTypes.INTEGER
}, {});
goods_to_category.associate = function(models) {
goods_to_category.belongsTo(models.goods, {foreignKey: 'goods_id'})
goods_to_category.belongsTo(models.category, {foreignKey: 'category_id'})
};
return goods_to_category;
};
делаю такой контролер
const models= require('../models');
exports.index = (req, res)=> {
models.goods.findAll().then((goods)=>{
for (val in goods){
console.log(val.category);
}
res.render('home',{
goods: goods
});
});
}
но вот в console.log получаю undefinded почему не могу понять