javascript - 为什么我只得到一个与此模型的关联?

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

对 Node.js 使用 Sequelize 我已经定义了一些关系。唯一的问题是 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"
      }
    ]
}

最佳答案

您将外键指定为 gamePlayerLogId但这是 gamePlayerLog 中的主键表所以只会有一个具有相应 id 的。我认为您可能想要的是在 gamePlayerLog 中指定促销 ID表作为外键。

关于javascript - 为什么我只得到一个与此模型的关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35076756/

相关文章:

javascript - 使时间密集型函数异步

php - Codeigniter 表单验证外部回调

jquery - 思考狮身人面像多个类

javascript - React 中每个组件的微调器

javascript - JSON 中的 HTML 标签不允许解析它

javascript - 更新 Parse 关系的 View Relations 字段

node.js - Mongoose :查找与条件不匹配的文档和修剪数组元素

node.js - 无法在 Electron 中显示文件夹选择对话框

ios - 大模型 TableView 解决方案 Swift

javascript - 具有冲突值的多个 'X-Frame-Options' header