Wednesday 7 June 2017

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?



via Mason Smith

No comments:

Post a Comment