Node.js 归属关系-续集
我在使用sequelize+nodejs列出与belongTomany关联的记录时遇到问题 我有开发者和技术表。他们之间的关系是多对多的。要创建关系,请使用名为developers\u technologies的透视表 下面是代码的要点:模型(开发者、技术)、迁移(开发者、技术)和开发者的控制器 控制器开发人员索引的代码如下:Node.js 归属关系-续集,node.js,api,rest,express,sequelize.js,Node.js,Api,Rest,Express,Sequelize.js,我在使用sequelize+nodejs列出与belongTomany关联的记录时遇到问题 我有开发者和技术表。他们之间的关系是多对多的。要创建关系,请使用名为developers\u technologies的透视表 下面是代码的要点:模型(开发者、技术)、迁移(开发者、技术)和开发者的控制器 控制器开发人员索引的代码如下: async index(req, res) { const developers = await Developer.findAll({ attributes: [
async index(req, res) {
const developers = await Developer.findAll({
attributes: ['id', 'name', 'email'],
include: [
{
model: Technologies,
as: 'technologies',
attributes: ['id', 'name'],
through: { attributes: [] },
},
],
});
return res.json(developers);}
返回:
[
{
"id": 25,
"name": "Jon Doe",
"email": "jondoe@gmail.com",
"age": 27,
"url_linkedin": "http://asdasdasd",
"technologies": []
}
]
我想知道为什么我没有收到与开发人员链接的技术,因为数据库中的developers\u technologies表已经填充,并且模型中的关系还可以
编辑:
根据请求生成的查询:
SELECT
"Developer"."id",
"Developer"."name",
"Developer"."email",
"technologies"."id" AS "technologies.id",
"technologies"."name" AS "technologies.name",
"technologies->developers_technologies"."created_at" AS "technologies.developers_technologies.createdAt",
"technologies->developers_technologies"."updated_at" AS "technologies.developers_technologies.updatedAt",
"technologies->developers_technologies"."developer_id" AS "technologies.developers_technologies.developer_id",
"technologies->developers_technologies"."technology_id" AS "technologies.developers_technologies.technology_id"
FROM "developers" AS "Developer"
LEFT OUTER JOIN ( "developers_technologies" AS "technologies->developers_technologies"
INNER JOIN "technologies" AS "technologies" ON "technologies"."id" = "technologies->developers_technologies"."developer_id") ON "Developer"."id" = "technologies->developers_technologies"."technology_id";
亲切问候,尝试在
中的项中指出属于许多关联的其他键。您还指示了错误的外键。对于开发者,它应该是Developer\u id
,对于技术,它应该是Technology\u id
:
模型开发人员
this.belongsToMany(models.Technology, {
foreignKey: 'developer_id',
otherKey: 'technology_id',
through: 'developers_technologies',
as: 'technologies',
});
模型技术
this.belongsToMany(models.Developer, {
foreignKey: 'technology_id',
otherKey: 'developer_id',
through: 'developers_technologies',
as: 'developers',
});
我补充道,相同的返回继续:/Very-square.。查看生成的SQL查询并将其添加到您的问题中