Mason Smith
Mason Smith

Reputation: 115

Node JS Sequelize sql associations

So still trying to figure out how to work associations and work with sequelize. Iam trying to associate a game that has multiple teams.

Game:

"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;
};

Team:

"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;
};

Would it be team belongsToMany games and games hasMany teams?

Upvotes: 0

Views: 253

Answers (2)

Nilesh Maharjan
Nilesh Maharjan

Reputation: 65

Here is my code to work with association in sequelize.

game.js

import DataTypes from 'sequelize';

    const 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
            },
        },
        {   
            tablenName: 'game'
            timestamps: false,
            freezeTableName: true;

});

module.exports = game;

team.js

import DataTypes from 'sequelize';

const team = sequelize.define('team', {
            teamId: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false
            },
            city: {
                type: DataTypes.STRING,
                allowNull: false
            }
        },
        {

            tableName: 'team',
            freezeTableName: true,
            timestamps: false,
});

module.exports = team;

//index.js (Association)

import game from '../models/game';
import team from '../models/team';

game.belongsTo(team, {foreignKey: 'awayTeam', targetKey: 'teamId'});
game.belongsTo(team, {foreignKey: 'homeTeam', targetKey: 'teamId'});

Upvotes: 0

Julien Klepatch
Julien Klepatch

Reputation: 348

You need to use hasMany() function, inside associate() class method.

Game:

"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'});
};

For this to work, you will also need to setup Sequelize this way: https://github.com/jklepatch/kangaroojs/blob/master/apps/web/models/index.js

If you also want to be able to use the association from the team model, you would need to use 'belongsTo()` in the team model

"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);
};

Upvotes: 2

Related Questions