A Sequelize column that cannot be updated

前端 未结 1 1322
面向向阳花
面向向阳花 2021-02-20 10:50

Is it possible to create a column on a MySQL table using Sequelize that can be initialized when creating a new row, but never updated?

For example, a REST service allows

1条回答
  •  失恋的感觉
    2021-02-20 11:23

    Look at this Sequelize plugin:

    https://www.npmjs.com/package/sequelize-noupdate-attributes

    It adds support for no update and read-only attributes in Sequelize models.

    In your specific case, you could configure the attribute with the following flags:

    {
      title: {
        type: DataTypes.STRING,
        allowNull: false,
        unique   : true,
        noUpdate : true
      }
    }
    

    That will allow the initial set of the title attribute if is null, and then prevent any further modifications once is already set.

    Disclaimer: I'm the plugin author.

    0 讨论(0)
提交回复
热议问题