我需要在 sequelize 中进行迁移以更改列。如何根据另一列的值对列使用 allowNull 约束?
例如,考虑我有 A 列和 B 列。在迁移中,我想要如下内容:
queryInterface.changeColumn('book', ['A'], {
allowNull: false,
where: { B: true }
});
但正如我在示例中看到的,我们不能在 changeColumn 中使用“where”。
最佳答案
我认为你必须像这样使用 customValidator 来解决这个问题:
queryInterface.changeColumn('book', ['A'], {
allowNull: true,
validate: {
customValidator(value) {
if (value === null && this.B) {
throw new Error("A not be null if B === true");
}
}
}
});
关于orm - 有条件地对allownull约束进行后续处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63330913/