gpt4 book ai didi

node.js - Sequelize : Populate the records with other table?

转载 作者:行者123 更新时间:2023-12-03 22:37:56 25 4
gpt4 key购买 nike

我是在我的 Node 应用程序中使用 PostgreSQLSequelize 的新手(使用 Hapi.js 框架)。

想要使用 Postgressequelize 从两个表中获取所有记录:
我有两个表 EventsContents 如下所示:

eventModel.js 如下:

'use strict';
module.exports = function (Sequelize, DataTypes) {
const Events = Sequelize.define('Events', {
id: { allowNull: false, type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
title: { allowNull: false, type: DataTypes.TEXT },
createdAt: { type: DataTypes.DATE },
updatedAt: { type: DataTypes.DATE },
}, { timestamps: true,
classMethods: {
associate: function (models) {
Events.belongsTo(models.Contents, { foreignKey : ‘event’ });
}
}
});
return Events;
};

事件表中的数据为:
id     title              createdAt                  updatedAt

1      Event1     2019-03-10T09:50:06.481Z 2019-03-10T09:50:06.481Z
2      Event2     2019-03-10T09:50:06.481Z 2019-03-10T09:50:06.481Z
3      Event3     2019-03-10T09:50:06.481Z 2019-03-10T09:50:06.481Z
4      Event4     2019-03-10T09:50:06.481Z 2019-03-10T09:50:06.481Z

contentModel.js 如下:
'use strict';
module.exports = function (Sequelize, DataTypes) {
const Contents = Sequelize.define(‘Contents', {
id: { allowNull: false, type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
description: { allowNull: false, type: DataTypes.TEXT},
eventId: { allowNull: false, type: DataTypes.INTEGER },
createdAt: { type: DataTypes.DATE },
updatedAt: { type: DataTypes.DATE },
}, { timestamps: true,
classMethods: {
associate: function (models) {
Contents.hasMany(models.Events, { foreignKey : 'id' });
}
}
});
return Contents;
};

以及具有以下记录的内容:
id     description     event     createdAt                        updatedAt

1      description1    1     2019-04-10T09:50:06.481Z. 2019-04-10T09:50:06.481Z
2     description2     1    2019-04-10T09:50:06.481Z. 2019-04-10T09:50:06.481Z
3     description3     2     2019-04-10T09:50:06.481Z. 2019-04-10T09:50:06.481Z
4     description4     2    2019-04-10T09:50:06.481Z. 2019-04-10T09:50:06.481Z

现在我想得到如下结果:
(当我从 Contents 表中获取所有记录时,我想根据 eventID 填充特定事件)
[
{
id:1,
description:”description1”,
createdAt:”2019-04-10T09:50:06.481Z”,
updatedAt:”2019-04-10T09:50:06.481Z”
eventId:{
id:1,
title:”Event1”,
description:”description1”,
createdAt:”2019-03-10T09:50:06.481Z”,
updatedAt:”2019-03-10T09:50:06.481Z”
}
},
{
id:2,
description:”description1”,
createdAt:”2019-04-10T09:50:06.481Z”,
updatedAt:”2019-04-10T09:50:06.481Z”
eventId:{
id:1,
title:”Event1”,
description:”description1”,
createdAt:”2019-03-10T09:50:06.481Z”,
updatedAt:”2019-03-10T09:50:06.481Z”
}
},
{
id:3,
description:”description1”,
createdAt:”2019-04-10T09:50:06.481Z”,
updatedAt:”2019-04-10T09:50:06.481Z”
eventId:{
id:12
title:”Event2”,
description:”description2”,
createdAt:”2019-03-10T09:50:06.481Z”,
updatedAt:”2019-03-10T09:50:06.481Z”
}
},
{
id:4,
description:”description1”,
createdAt:”2019-04-10T09:50:06.481Z”,
updatedAt:”2019-04-10T09:50:06.481Z”
eventId:{
id:12
title:”Event2”,
description:”description2”,
createdAt:”2019-03-10T09:50:06.481Z”,
updatedAt:”2019-03-10T09:50:06.481Z”
}
}

]

为了得到想要的结果,我写了如下查询:
const Models = require('./../../../models/'); // declared all models here correctly.
var getRecords= function(){
Models.Contents.findAll({ where: {},
include: [{ model: Models.Events, where: {} } ]
  }).then((data) => {
    console.log(data);
      });
}

但我没有得到我想要的。

谁能建议我哪里做错了以及我需要做什么才能得到想要的结果?

最佳答案

乍一看,它看起来像是 Event 模型的关联问题,您声明它
像这样 foreignKey: 'event' 的显式名称
Events.belongsTo(models.Contents, { foreignKey : ‘event’ });
但是您在 DB 和 eventId 模型中使用了 Contents

你可以明确地写 Events.belongsTo(models.Contents, { foreignKey : ‘eventId’ });或者只是 Events.belongsTo(models.Contents); 因为默认情况下 sequelize 将映射到 eventId 作为外键。

关于node.js - Sequelize : Populate the records with other table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645977/

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