Sequelize:用其他表填充记录吗?

时间:2019-04-12 06:48:48

标签: node.js postgresql sequelize.js

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

想使用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

现在我想得到的结果为: (当我从目录表中获取所有记录时,我想根据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);
                       });
      } 

但是我没有得到想要的东西。

有人可以建议我做错了什么,以及需要做些什么才能获得期望的结果?

1 个答案:

答案 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。