[Sequelize.Op.regexp]: '^[a-zA-Z0-9]{2}\d{10}$' not working with queryInterface.addConstraint sequelize #15436
-
I need to add constraint(regex) on DB level so that no one able to enter/edit that particular column with invalid input on already existing table. I'm using Sequelize, Postgres. I tried it using migration but still able to enter invalid value in DB. I was expecting that after running below migration I wouldn't able to enter/edit that column with wrong input but not working. Attaching my code below for you better understanding. `module.exports = { async down (queryInterface, Sequelize) { ERROR: Fields must be specified through options.fields |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hello, the error message displays that it is necessary to specify the async up (queryInterface, Sequelize) {
await queryInterface.addConstraint('Table', ['column'], {
type: 'check',
name: 'checkNumber',
fields: ['column'], // insert this fields definition
where: {
column: {
[Sequelize.Op.regexp]: '^[a-zA-Z0-9]{2}\d{10}$'
}
}
});
},
async down (queryInterface, Sequelize) {
await queryInterface.removeConstraint('Table', 'checkNumber');
}
}; As you left only a snippet of code, I could not reproduce and test this solution, but it is in accordance with what is defined in the Sequelize documentation. See if this solution helps you. |
Beta Was this translation helpful? Give feedback.
Hello, the error message displays that it is necessary to specify the
options.fields
, so I think that's the problem.Try to set these fields and I believe this could be an solution. Considering your code make this change:
As you left only a snippe…