Person Dev
Person Dev

Reputation: 11

Issue with enum data type in create function

I have created "blogs" table with id(primary key), title ,created at ,updated at , status (with enum type having values "active,inactive,hidden" with default value "active")

create function on Blog Model is working fine when giving status value from above mentioned set of values and empty value

const Blog = sequelize.define('blog', { id: { type: 
Sequelize.INTEGER, 
primaryKey: true, autoIncrement: true }, text: Sequelize.STRING, 
status : { type : Sequelize.ENUM, allowNull : false, values : 
['Inactive','Active','Hidden'], defaultValue : 'Active' } }); 
Blog.create({"title" :"hello","status":"abc"}).then(result => {
console.log(result);
});

The above code inserted a new record in blogs table with status of empty value.but result object having status of "abc".How can I get newly inserted record ?

Upvotes: 1

Views: 1304

Answers (1)

rkm
rkm

Reputation: 3141

You should define ENUM like this:

status : { 
  type : Sequelize.ENUM('Inactive','Active','Hidden'), 
  allowNull : false, 
  defaultValue : 'Active',
  validate: {
    isIn: { 
      args: [['Inactive','Active','Hidden']],
      msg: "Wrong status"
    }
  }
}

In order to validate enum values you can use per attribute validation

Upvotes: 3

Related Questions