sql - Node JS Sequelize sql 关联

标签 sql node.js sequelize.js

所以仍然试图弄清楚如何使用关联和 Sequelize 。我正在尝试关联具有多个团队的游戏。

游戏:

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

会不会是团队属于许多游戏而游戏有很多团队?

最佳答案

您需要在 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);
};

关于sql - Node JS Sequelize sql 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44426375/

相关文章:

node.js - 我无法通过 sequelize 进行基本的 sql 查询

sql - SQL中的表扫描和索引扫描

javascript - GYP 在 pcduino 上安装 Node 时出错

mysql - 自定义 AUTO INCREMENT 值不起作用

json - 无法运行react js项目

node.js - fs.readdirSync 卡住 Electron 应用程序

sequelize.js - Sequelize 迁移是否应该更新模型文件?

javascript - 在中间表中续写多对多关系和一对多关系

sql - 删除最新的冗余行并更新时间戳

mysql - 我是否在我的 SQL 语句中运行了太多子查询?