2017-06-08 15 views
0

アソシエーションをどのように動作させ続けるかを理解しようとしています。 Iamは複数のチームを持つゲームを関連づけようとしています。ノードJS SQLの関連付けを続ける

ゲーム:

"use strict" 
module.exports = (sequelize, DataTypes) => { 
    var game = sequelize.define('game', { 
      gameId: { 
       type: DataTypes.INTEGER, 
       primaryKey: true, 
       autoIncrement: true 
      }, 
      awayTeam: { 
       type: DataTypes.INTEGER, 
       references: { 
        model: "team", 
        key: 'teamId', 
       }, 
       allowNull: false 
      }, 
      homeTeam: { 
       type: DataTypes.INTEGER, 
       references: { 
        model: "team", 
        key: 'teamId', 
       }, 
       allowNull: false 
      }, 
      awayTeamScore: { 
       type: DataTypes.INTEGER, 
       allowNull: false 
      }, 
      homeTeamScore: { 
       type: DataTypes.INTEGER, 
       allowNull: false 
      }, 
     }, 
     { 
      timestamps: false, 
      tableName: 'game', 
       associate: function (models) { 
       /// trying to figure this out 
      } 
     } 
    ); 
    return game; 
}; 

チーム:

"use strict" 
module.exports = (sequelize, DataTypes) => { 
    var team = sequelize.define('team', { 
      teamId: { 
       type: DataTypes.INTEGER, 
       primaryKey: true, 
       autoIncrement: true 
      }, 
      name: { 
       type: DataTypes.STRING, 
       allowNull: false 
      }, 
      city: { 
       type: DataTypes.STRING, 
       allowNull: false 
      } 
     }, 
     { 
      timestamps: false, 
      tableName: 'team', 
      associate: function(models){ 
       /// trying to figure this out 
      } 
     } 
    ); 

    return team; 
}; 

それはチームbelongsToManyゲームやゲームhasManyのチームでしょうか?

答えて

2

hasMany()ファンクション、associate()クラスメソッドを使用する必要があります。

ゲーム:このため

"use strict"; 

var game = {}; 
var classmethods = {}; 

module.exports = (sequelize, DataTypes) => { 
    game = sequelize.define('game', { 
      gameId: { 
       type: DataTypes.INTEGER, 
       primaryKey: true, 
       autoIncrement: true 
      }, 
      awayTeam: { 
       type: DataTypes.INTEGER, 
       references: { 
        model: "team", 
        key: 'teamId', 
       }, 
       allowNull: false 
      }, 
      homeTeam: { 
       type: DataTypes.INTEGER, 
       references: { 
        model: "team", 
        key: 'teamId', 
       }, 
       allowNull: false 
      }, 
      awayTeamScore: { 
       type: DataTypes.INTEGER, 
       allowNull: false 
      }, 
      homeTeamScore: { 
       type: DataTypes.INTEGER, 
       allowNull: false 
      }, 
     }, 
     { 
      timestamps: false, 
      classmethods: classmethods 
     } 
    ); 
    return game; 
}; 

classmethods.associate = function (models) { 
    game.hasMany(models.team, {foreignKey: 'teamId'}); 
}; 

仕事に、あなたはまた、セットアップSequelizeにこの方法が必要になります。 https://github.com/jklepatch/kangaroojs/blob/master/apps/web/models/index.js

あなたもチームモデルから関連付けを使用することができるようにしたい場合は、チームモデルで'belongsTo()`を使用する必要があります

"use strict"; 

var team; 
var classmethods; 

module.exports = (sequelize, DataTypes) => { 
    team = sequelize.define('team', { 
      teamId: { 
       type: DataTypes.INTEGER, 
       primaryKey: true, 
       autoIncrement: true 
      }, 
      name: { 
       type: DataTypes.STRING, 
       allowNull: false 
      }, 
      city: { 
       type: DataTypes.STRING, 
       allowNull: false 
      } 
     }, 
     { 
      timestamps: false, 
      tableName: 'team', 
      classmethods: classmethods 
     } 
    ); 

    return team; 
}; 

classmethods.associate = function (models) { 
    game.belongsTo(models.game); 
}; 
関連する問題