gpt4 book ai didi

sequelize.js - Feathers-Sequelize:hasMany 只返回第一个结果,展平

转载 作者:行者123 更新时间:2023-12-01 11:16:59 29 4
gpt4 key购买 nike

我今天第一次尝试了羽毛 Sequelize ,现在已经在 hasMany 协会上挣扎了几个小时。

我想做一个简单的projects (>hasMany>) issues关系,但我只是得到第一个问题。

期望输出:

{
"id": 1,
"name": "Project 1",
"issues": [
{"id": 1,
"title": "Issue 1"},
{"id": 2,
"title": "Issue 2"},
]
}

实际输出:
{
"id": 1,
"name": "Project 1",
"issues.id": 1,
"issues.title": "Issue 1",
}

我使用 feathers-cli (v3.6.1) 生成了服务,并添加了 before-hook 以包含问题。我的模型看起来像这样

项目.model.js:
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const projects = sequelizeClient.define('projects', {
id: { type: Sequelize.INTEGER(10).UNSIGNED, autoIncrement: true, primaryKey: true},
name: Sequelize.STRING,
},
{
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});

projects.associate = function(models) {
projects.hasMany(models.issues);
};
}

问题.model.js
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const issues = sequelizeClient.define('issues', {
id: {
type: Sequelize.INTEGER(10).UNSIGNED,
autoIncrement: true,
primaryKey: true
},
project_id: {
type: Sequelize.INTEGER(10).UNSIGNED,
references: { model: 'projects' },
onDelete: 'CASCADE',
},
title: Sequelize.STRING,
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});

issues.associate = function (models) {
issues.belongsTo(models.projects);
};
return issues;
};

我在这里遗漏了一些明显的东西吗?

最佳答案

找到了答案。问题不在于模型定义,而在于包含问题模型的钩子(Hook)。需要参数原始:假

var includeIssues = (hook) => {
if (hook.params.query.include) {
const IssueModel = hook.app.services.issues.Model;
//
hook.params.sequelize = {
raw: false,
include: [
{model: IssueModel}
]
};
// delete any special query params so they are not used
// in the WHERE clause in the db query.
delete hook.params.query.include;

return Promise.resolve(hook);
}
};

关于sequelize.js - Feathers-Sequelize:hasMany 只返回第一个结果,展平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580851/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com