Sequelize - findOrCreate在“通过”表上,在belongsToMany关联

时间:2016-02-29 19:21:20

标签: javascript node.js sequelize.js

首先,我对Node.JS很新,甚至比Sequelize更新,这一直困扰着我。我有以下模型实体:

Match.js

module.exports = function(sequelize, DataTypes) {
    var Match = sequelize.define('Match', {
            matchId: {
                type: DataTypes.BIGINT,
                field: 'match_id'
            },
            server: {
            type: DataTypes.STRING
            },
            (...)
            rankedBoo: {
                type: DataTypes.BOOLEAN,
                field: 'ranked_boo'
            }
    }, {
        classMethods: {
            associate: function(models) {
                Match.belongsToMany(models.Summoner, {as: 'Participants', through: 'SummonerMatch'});
            }
        },
        freezeTableName: true,
        underscored: true
    });

  return Match;
};

Summoner.js

module.exports = function(sequelize, DataTypes) {
  var Summoner = sequelize.define('Summoner', {
        summonerId: {
            type: DataTypes.INTEGER,
            field: 'summoner_id'
        },
        server: {
            type: DataTypes.STRING
        },
        summonerName: {
            type: DataTypes.STRING,
            field: 'summoner_name'
        },
        mainChampion: {
            type: DataTypes.INTEGER,
            field: 'main_champion'
        }
    }, {
        classMethods: {
            associate: function(models) {
                Summoner.belongsToMany(models.Match, {as: 'SummonerMatches', through: 'SummonerMatch'});
                Summoner.hasOne(models.RankedStats);
                Summoner.hasMany(models.RankedHistory, { as: { singular: 'RankedHistory', plural: 'RankedHistory' }});
            }
        },
        freezeTableName: true,
        underscored: true
    });


  return Summoner;
};

SummonerMatch.js

module.exports = function(sequelize, DataTypes) {
    var SummonerMatch = sequelize.define('SummonerMatch', {
        championId: {
            type: DataTypes.INTEGER,
            field: 'champion_id'
        },
        role: {
            type: DataTypes.STRING
        },
        (...)
        sightWardsBought: {
            type: DataTypes.INTEGER,
            field: 'sight_wards_bought'
        }
    }, {
        freezeTableName: true,
        underscored: true
    });

  return SummonerMatch;
};

现在我正在尝试创建一个新的匹配,并将它与召唤师联系起来,我正在做以下事情:

summoner.createSummonerMatch(matchInfo, matchDetails).then(
    function () {
        callback();
        return null;
});

其中 matchInfo 包含“匹配”实体的属性, matchDetails 包含“SummonerMatch”实体的属性。

这很好,但是它没有检查匹配是否已经存在,所以我在这里尝试使用 findOrCreate

models.Match.findOrCreate({
    include: [ {model: models.Summoner, as: 'Participants'}],
    where: { matchId: matchInfo.matchId, server: matchInfo.server },
    defaults: {
        matchMode: queueType,
        matchDate: new Date(matchCreation),
        matchDuration: matchDurationInSeconds,
        rankedBoo: rankedBoo
        }
    }).spread(function(match, created) {
        console.log(created)
});

这几乎可以解决问题(在Match表中创建一个新行,但在SummonerMatch中没有)。我如何继续将信息插入SummonerMatch?尝试了一些事情(将属性添加到默认值,将其切换到数组,使用include调整,但到目前为止没有成功。

我很遗憾地错过了什么,但我无法弄清楚是什么。非常感谢任何帮助:)

[编辑]如果有人来这里寻找如何做的答案,这是有效的,但我不确定这是否是最好的方法:

models.Match.findOrCreate({
    include: [ {model: models.Summoner, as: 'Participants'}],
    where: { matchId: matchInfo.matchId, server: matchInfo.server },
    defaults: {
        matchMode: queueType,
        matchDate: new Date(matchCreation),
        matchDuration: matchDurationInSeconds,
        rankedBoo: rankedBoo
        }
    }).spread(function(match, created) {
        match.addParticipant(summoner, matchDetails).then(
            function () {
                console.log('Done')
            }
        )
});

如果它不存在则创建匹配,然后添加参与者。有没有办法一次完成所有工作?

0 个答案:

没有答案