admin管理员组

文章数量:1278788

Is it possible to change the "validate" meta data of a column using a migration file? I tried the queryInterface.changeColumn method and it seems like it can only change the three meta data mentioned in the docs (defaultValue, allowNull, and type).

I've tried doing something like this inside the 'up' object of the migration file:

queryInterface.changeColumn(
  'tableName',
  'columnName',
  {
    validate: {
      is: /new_regex_validation/
    }
  }
)

However, the above attempt did not work for me when I ran "sequelize db:migrate"

For simplicity's sake, I'll use a table definition to elaborate on my question:

I'm trying to change an already existing table like this:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false,
        validate: {
            is: /some_regex_validation/
        }
    }
})

into this using sequelize migration:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false,
        validate: {
            is: /a_new-or-different_regex_validation/
        }
    }
})

or simply remove the validate meta data while using sequelize migration:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false
    }
})

Any ideas?

Is it possible to change the "validate" meta data of a column using a migration file? I tried the queryInterface.changeColumn method and it seems like it can only change the three meta data mentioned in the docs (defaultValue, allowNull, and type).

I've tried doing something like this inside the 'up' object of the migration file:

queryInterface.changeColumn(
  'tableName',
  'columnName',
  {
    validate: {
      is: /new_regex_validation/
    }
  }
)

However, the above attempt did not work for me when I ran "sequelize db:migrate"

For simplicity's sake, I'll use a table definition to elaborate on my question:

I'm trying to change an already existing table like this:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false,
        validate: {
            is: /some_regex_validation/
        }
    }
})

into this using sequelize migration:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false,
        validate: {
            is: /a_new-or-different_regex_validation/
        }
    }
})

or simply remove the validate meta data while using sequelize migration:

var tableName = sequelize.define('tableName', {
    columnName: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false
    }
})

Any ideas?

Share Improve this question edited Oct 2, 2016 at 0:24 JV Estolas asked Oct 2, 2016 at 0:17 JV EstolasJV Estolas 914 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Validation happens on the client, not on the database. You don't need a migration for it.

I came across this question a few times trying to understand the difference between building my model with validations and using migrations. I found this link very helpful. Hopefully someone else does in the future.

Difference between Validations and Constraints

Validations are checks performed in the Sequelize level, in pure JavaScript. They can be arbitrarily plex if you provide a custom validator function, or can be one of the built-in validators offered by Sequelize. If a validation fails, no SQL query will be sent to the database at all.

On the other hand, constraints are rules defined at SQL level. The most basic example of constraint is an Unique Constraint. If a constraint check fails, an error will be thrown by the database and Sequelize will forward this error to JavaScript (in this example, throwing a SequelizeUniqueConstraintError). Note that in this case, the SQL query was performed, unlike the case for validations.

https://sequelize/master/manual/validations-and-constraints.html

本文标签: javascriptSequelize Change quotvalidatequot meta data of columnStack Overflow