我是在节点应用程序(使用Hapi.js框架)中使用PostgreSQL
和Sequelize
的新手。
想使用Postgres
和sequelize
从两个表中获取所有记录:
我有两个表Events
和Contents
如下所示:
如下所示的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
现在我想得到的结果为: (当我从目录表中获取所有记录时,我想根据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);
});
}
但是我没有得到想要的东西。
有人可以建议我做错了什么,以及需要做些什么才能获得期望的结果?
答案 0 :(得分:0)
乍一看,您似乎Event
模型的关联存在问题,
像这样foreignKey: 'event'
Events.belongsTo(models.Contents, { foreignKey : ‘event’ });
但是您正在数据库和eventId
模型中使用Contents
。
您可以明确地写Events.belongsTo(models.Contents, { foreignKey : ‘eventId’ });
或仅Events.belongsTo(models.Contents);
,因为默认情况下sequelize
会映射为eventId作为ForeignKey。