所以仍然试图弄清楚如何工作协会和使用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;
};
是团队所属的许多游戏和游戏都有很多团队吗?
答案 0 :(得分:2)
您需要在associate()
类方法中使用hasMany()
函数。
游戏:
"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);
};
答案 1 :(得分:0)
这是我的代码,可与续集一起使用。
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(关联)
import game from '../models/game';
import team from '../models/team';
game.belongsTo(team, {foreignKey: 'awayTeam', targetKey: 'teamId'});
game.belongsTo(team, {foreignKey: 'homeTeam', targetKey: 'teamId'});