Mysql 如何从Sequalize生成的模型中获取相关数据?
我一直在开发一个支持MySQL的NodeJs后端应用程序,使用Sequelize作为ORM。我试图通过调用我创建的API来获取数据。它将数据作为响应发送。但是它不包含与外键关系相关联的对象 我已经有了一个MySQL数据库,我正在使用sequelize ORM,我使用sequelize auto生成模型类。所有模型类都已成功生成。但这些关联并不是由模型产生的。因此,为了迎合这些关联,我必须手动将这些关联添加到模型类中。然后我创建路由文件并创建HTTPGET方法。但是API端点没有按预期发送数据 下面显示了我创建的模型类和管线文件Mysql 如何从Sequalize生成的模型中获取相关数据?,mysql,node.js,orm,sequelize.js,associations,Mysql,Node.js,Orm,Sequelize.js,Associations,我一直在开发一个支持MySQL的NodeJs后端应用程序,使用Sequelize作为ORM。我试图通过调用我创建的API来获取数据。它将数据作为响应发送。但是它不包含与外键关系相关联的对象 我已经有了一个MySQL数据库,我正在使用sequelize ORM,我使用sequelize auto生成模型类。所有模型类都已成功生成。但这些关联并不是由模型产生的。因此,为了迎合这些关联,我必须手动将这些关联添加到模型类中。然后我创建路由文件并创建HTTPGET方法。但是API端点没有按预期发送数据 下
module.exports = function(sequelize, DataTypes) {
const Department = sequelize.define('Department', {
department_id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(100),
allowNull: false
},
description: {
type: DataTypes.STRING(1000),
allowNull: true
}
}, {
tableName: 'department',
timestamps: false,
underscored: true
});
Department.associate = function(models) {
// associations can be defined here
Department.hasMany(models.Category, {
foreignKey: 'department_id',
as: 'categories',
});
};
return Department;
};
部门/路线的回应
{
"error": false,
"data": [
{
"department_id": 1,
"name": "Regional",
"description": "Proud of your country? Wear a T-shirt with a national symbol stamp!"
},
{
"department_id": 2,
"name": "Nature",
"description": "Find beautiful T-shirts with animals and flowers in our Nature department!"
},
{
"department_id": 3,
"name": "Seasonal",
"description": "Each time of the year has a special flavor. Our seasonal T-shirts express traditional symbols using unique postal stamp pictures."
}
]
}
类别/路线的响应
{
"data": [],
"error": true
}
我希望数据也与相关对象一起提供。但它没有按预期发送数据。我在这里做错了什么。这里的问题是,您在查询中没有说您需要关联数据。为此,您必须在
findAll()函数中使用include
/* GET departments listing. */
router.get('/', function(req, res, next) {
model.Department.findAll({
include: [{
model: model.Category
as: 'categories'
}]
})
.then(department => res.json({
error: false,
data: department
}))
.catch(error => res.json({
data: [],
error: true
}));
});
编辑:
我看到您更改了模型的默认主键,因此您还必须在类别关联中指定该主键。Sequelize默认情况下仅适用于单向关联,适用于从部门
到类别
的案例。Sequelize不知道部门
类别
上的外键,即使您定义了它。您必须定义要指向的关键点
Category.associate = function(models) {
// associations can be defined here
Category.belongsTo(models.Department, {as: 'department', foreignKey: 'department_id'})
};
谢谢,但当我在fetchall中添加include参数时,这就是它生成的查询。查询无效。选择
Category
Category\u id
,Department\u id
作为Department.Department\u id
,Department
作为Department.name
,将,部门
描述作为部门。描述
来自类别
作为类别
左侧外部连接部门
作为部门
关于类别
部门id=部门
部门id
;Category.department\u department\u id此部分是错误的,因为没有名为“department\u department\u id”的列如何修复此问题谢谢,你救了我一天!!
{
"data": [],
"error": true
}
/* GET departments listing. */
router.get('/', function(req, res, next) {
model.Department.findAll({
include: [{
model: model.Category
as: 'categories'
}]
})
.then(department => res.json({
error: false,
data: department
}))
.catch(error => res.json({
data: [],
error: true
}));
});
Category.associate = function(models) {
// associations can be defined here
Category.belongsTo(models.Department, {as: 'department', foreignKey: 'department_id'})
};