2

I have created a controller that to adding new value into the database, but now I do not know how to set up the default value into Sequelize. Let me show my code below.

Model User:

module.exports = (sequelize, Sequelize) => {
  const User = sequelize.define("user", {
    user_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
    username: {
      type: Sequelize.STRING,
    },
    password: {
      type: Sequelize.STRING,
    },
    full_name: {
      type: Sequelize.STRING,
    },
    email: {
      type: Sequelize.STRING,
    },
    role_id: {
      type: Sequelize.STRING,
    },
    is_active: {
      type: Sequelize.BOOLEAN,
    },
    created_date: {
      type: Sequelize.DATE,
    },
    created_by: {
      type: Sequelize.STRING,
    },
    updated_date: {
      type: Sequelize.DATE,
    },
    updated_by: {
      type: Sequelize.STRING,
    },
  });

  return User;
};

Model Role:

module.exports = (sequelize, Sequelize) => {
  const Role = sequelize.define("role", {
    role_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
    role_name: {
      type: Sequelize.STRING,
    },
    is_active: {
      type: Sequelize.BOOLEAN,
    },
    created_date: {
      type: Sequelize.DATE,
    },
    created_by: {
      type: Sequelize.STRING,
    },
    updated_date: {
      type: Sequelize.DATE,
    },
    updated_by: {
      type: Sequelize.STRING,
    },
  });

  return Role;
};

Controller User:

const db = require("../models");
const User = db.users;

exports.create = (req, res) => {
    // Validate request
    if (!req.body.username) {
      res.status(400).send({
        message: "Content can not be empty!"
      });
      return;
    }
  
    // Create a Tutorial
    const user = {
        username: req.body.username,
        password: req.body.password,
        full_name: req.body.full_name,
        email: req.body.email,
        role_id: req.body.role_id,
        is_active: req.body.is_active,
        created_date: req.body.created_date,
        created_by: req.body.created_by,
        updated_date: req.body.updated_date,
        updated_by: req.body.updated_by,
    };
  
    // Save Tutorial in the database
    User.create(user)
      .then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while creating new User."
        });
      });
  };

How can I add the default value that every value MUST BE Role is USER and is_active = 1?

1 Answer 1

3

You can define the default value at the database level in your migration file. Or, you can also define at sequelize level in your model definition itself.

module.exports = (sequelize, Sequelize) => {
  const User = sequelize.define("user", {
    user_id: {
      type: Sequelize.STRING,
      autoIncrement: true,
      primaryKey: true,
    },
role_name: {
      type: Sequelize.STRING,
      defaultValue: 'USER'
    },
    is_active: {
      type: Sequelize.BOOLEAN,
      defaultValue: 1
    }
    });

  return User;
};

https://sequelize.org/docs/v6/core-concepts/model-basics/#default-values

Sign up to request clarification or add additional context in comments.

1 Comment

Just like that i do not need to make the controller , that was easy. Thank for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.