我有两个表:用户和警报。用户应该可以对警报进行upvote和downvote。
我的模型定义如下:
警报
module.exports = function(sequelize, DataTypes) {
var Alert = sequelize.define("alert", {
ID: { type: DataTypes.BIGINT(11).UNSIGNED, primaryKey: true, autoIncrement: true },
title: DataTypes.STRING,
alert_type: DataTypes.INTEGER
},
{
classMethods: {
associate: function(models) {
Alert.belongsTo(models.user, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Alert;
};
用户
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("user", {
ID: { type: DataTypes.BIGINT(11).UNSIGNED, primaryKey: true, autoIncrement: true },
user_login: DataTypes.STRING(50),
user_pass: DataTypes.STRING(50),
user_email: DataTypes.STRING(50),
},
{
classMethods: {
associate: function(models) {
User.hasMany(models.alert);
}
}
});
return User;
};
使用sequelize.js和MySQL(InnoDB)的方法是什么?
我应该使用belongsToMany()吗? 类似的东西:
module.exports = function(sequelize, DataTypes) {
var AlertVote = sequelize.define("user_alert_vote", {
ID: { type: DataTypes.BIGINT(11).UNSIGNED, primaryKey: true, autoIncrement: true },
vote_up: DataTypes.INTEGER,
vote_down: DataTypes.INTEGER
},
{
classMethods: {
// belongToMany(models.alert, ...);
// belongToMany(models.user, ...);
}
});
return AlertVote ;
};