我有一个巡回赛的续集模型,它有一个开头和一个结尾:
this.model = db.define('Tour', {
id: {
type: Sequelize.BIGINT(20),
allowNull: false,
autoIncrement: true,
primaryKey: true
},
from_time: {
type: Sequelize.DATE,
allowNull: true
},
to_time: {
type: Sequelize.DATE,
allowNull: true
}
}
如何确保to_time
在from_time
之后?
(我猜validate
可以使用,但我不知道如何)
答案 0 :(得分:6)
this
将引用当前模型。所以它可以写成:
// ...
to_time: {
type: Sequelize.DATE,
allowNull: true,
validate: {
isAfterFrom: function(toTime) {
if (this.fromTime > toTime) {
throw new Error('To time must be less then from time')
}
}
}
},
// ...