9

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?

JV Estolas
  • 91
  • 4

2 Answers2

12

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

felixfbecker
  • 2,273
  • 1
  • 19
  • 24
  • Thanks for answering! Confirmed this by editing the model file's validate meta data. It works! :D – JV Estolas Oct 04 '16 at 00:21
  • I would improve this answer by clarifying that the validation happens at the server layer and not at the database layer, therefore we don't need to tell our database (through the migration) what we are validating. – Alejandro Corredor Jul 09 '19 at 04:07
  • 1
    upvoted! does this answer still hold just curious, does this mean all validate clauses are only added in model files and not migration files – PirateApp Sep 20 '19 at 03:53
  • Does it mean we don't need any validation on backend :/ that's answer need to be down voted instead of up voted – Abdulhaq Shah Oct 18 '19 at 14:36
0

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 complex 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.org/master/manual/validations-and-constraints.html

James
  • 93
  • 2
  • 7
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/26022265) – David Buck May 03 '20 at 21:30
  • Thank you. I added an edit with the part that helped it click for me. – James May 03 '20 at 22:47