【问题标题】:setting the value of a new column according to the value of an existing column using Sequelize使用 Sequelize 根据现有列的值设置新列的值
【发布时间】:2016-09-09 22:54:37
【问题描述】:

这是现有的表格:

var Main = sequelize.define('Main', {
   id: {
    type: Sequelize.INTEGER(100),
    allowNull: true
    }
});

然后我添加一个新列:

var Main = sequelize.define('Main', {
   id: {
    type: Sequelize.INTEGER(100),
    allowNull: true
    },
   status: {
    type: Sequelize.INTEGER(100),
    allowNull: true
    }
});

我想将 status 的值设置为 id +1 的值,我尝试了如下的 setter 和 getter 定义,但它不起作用:

var Main = sequelize.define('Main', {
   id: {
    type: Sequelize.INTEGER(100),
    allowNull: true
    },

   status: {
    type: Sequelize.INTEGER(100),
    allowNull: true,
    get: function () {
             var num = getDataValue('id');
             return num;
        }, 
    set: function (val) {
             this.setDataValue('status',val+1);
        }
    }
});

我该怎么办?

【问题讨论】:

    标签: node.js database sequelize.js


    【解决方案1】:

    setter 期望您在创建记录时发送该列。 你可以:

    1. 为 status 发送一个空值并将您的 setter 更改为:

    set: function () {
      this.setDataValue('status', this.getDataValue('id') + 1);
    }
    1. 将您的设置器更改为 id 属性

    set: function (value) {
      this.setDataValue('status', value + 1);
    }

    【讨论】:

    • 但状态没有改变,如何让setter和getter工作?
    猜你喜欢
    • 2017-06-26
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多