Specify DataType length when defining sequelize models?


262 观看


31 作者的声誉

According to the sequelize API reference (http://docs.sequelizejs.com/variable/index.html), it says:

To provide a length for the data type, you can invoke it like a function: INTEGER(2)

Most of the examples I've seen do not specify the data type length when defining the models.

module.exports = (sequelize, DataTypes) => {
    return sequelize.define('MyModel', {
        field1: {
            type: DataTypes.STRING,

        // vs.

        field2: {
            type: DataTypes.STRING(20),

I know there is separate validation that can be done to restrict the length, so it begs the question...

Is it recommended to define the length for the property in the model? If so, why?

作者: philthathril 的来源 发布者: 2017 年 9 月 15 日

回应 1


31 作者的声誉


When defining the models, I decided to not specify the length of the field since I don't believe it has any benefit. To control the length specified when creating/updating the fields, I use the validate.len option.

However, when creating migrations, I do specify the length as one may expect.

作者: philthathril 发布者: 2017 年 12 月 11 日