为什么我只与这个模型建立联系?

时间:2016-01-29 04:13:31

标签: javascript node.js model relational-database sequelize.js

使用Sequelize for Node.js我定义了一些关系。唯一的问题是promotion模型每个只包含一个gamePlayLog,而不是每个模型。下面的示例促销应该有超过100 gamePlayLogs个。我从返回的促销/锦标赛数组中复制了一个以供参考。我做错了吗?

var Sequelize = require('sequelize'),
    seqlz = new Sequelize('wegweg', 'wegwegweg', 'wegwegweg!', {
        host: '...',
        port: '3306',
        dialect: 'mysql',
        timezone: 'UTC-05:00',
        pool: {
            max: 5,
            min: 0,
            idle: 10000
        }
    }),
    Promotion = seqlz.define('promotion', {
        tournamentId: {
            type: Sequelize.BIGINT(20),
            primaryKey: true
        },
        iconFilePath: Sequelize.STRING,
        name: Sequelize.STRING(75),
        eventStartDate: Sequelize.DATE,
        eventEndDate: Sequelize.DATE
    }, {
        timestamps: false,
        tableName: 'Tournament'
    }),
    Advertiser = seqlz.define('advertiser', {
        advertiserId: {
            type: Sequelize.BIGINT(20),
            primaryKey: true
        },
        name: Sequelize.STRING(75),
    }, {
        timestamps: false,
        tableName: 'Advertiser'
    }),
    Game = seqlz.define('game', {
        gameId: {
            type: Sequelize.BIGINT(20),
            primaryKey: true
        },
        name: Sequelize.STRING(75)
    }, {
        timestamps: false,
        tableName: 'Game'
    }),
    GamePlayLog = seqlz.define('gamePlayLog', {
        gamePlayLogId: {
            type: Sequelize.BIGINT(20),
            primaryKey: true
        },
        createdDate: Sequelize.DATE
    }, {
        timestamps: false,
        tableName: 'GamePlayLog'
    });

Promotion.belongsTo(Advertiser, {foreignKey: 'advertiserId'});
Promotion.belongsTo(Game, {foreignKey: 'gameId'});
Promotion.hasMany(GamePlayLog, {foreignKey: 'gamePlayLogId'});

exports.handler = function(event, context) {
    Promotion.findAll({
        where: {status: 3},
        include: [Advertiser, Game, GamePlayLog]
    }).then(function(promos) {
        context.done(null, promos);
    });
};



{
    "tournamentId": 607,
    "iconFilePath": "uploadfiles/tournament/icon/...-mobile-icon.jpg",
    "name": "Win Custom Artwork",
    "eventStartDate": "2015-07-27T05:00:00.000Z",
    "eventEndDate": "2016-08-02T04:00:00.000Z",
    "advertiserId": 37,
    "gameId": 14,
    "advertiser": {
      "advertiserId": 37,
      "name": "...Customs"
    },
    "game": {
      "gameId": 14,
      "name": "Eggcetera"
    },
    "gamePlayLogs": [
      {
        "gamePlayLogId": 607,
        "createdDate": "2015-02-26T13:31:34.000Z"
      }
    ]
}

1 个答案:

答案 0 :(得分:0)

您将外键指定为gamePlayerLogId,但这是gamePlayerLog表中的主键,因此只会有一个具有相应ID的外键。我想您可能想要的是将gamePlayerLog表中的促销ID指定为外键。