How do I set a default value for a column in Sequelize?

Asked

Viewed 536 times

2

I am using Sequelize (v5.0+) with Nodejs (with Express). I want that for the column status in my database has the default value (defaultValue) as true.

Migration

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('users', {
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      status: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      }
    }
  down: (queryInterface) => {
    return queryInterface.dropTable('users');
  }
}

Model

import Sequelize, { Model } from 'sequelize';

class User extends Model {
  static init(sequelize) {
    super.init({
      status: Sequelize.BOOLEAN,
    },
    {
      sequelize,
    }
  );
}

export default User;

Controller

import User from '../models/User';

class UserController {
  async store(req, res) {
    const { id, status } = await User.create(req.body);

    return res.json({ message: `${id}, Status: ${status}, user register successful` });
  }

    return res.json({ users });
  }
}

export default new Users();

When I send my request without indicating the status, I am returned the following error:

SequelizeDatabaseError: null value in column "status" violates not-null constraint

If I put in the request body the value of the field status works normally.

I’ve already looked at the Sequelize documentation, but I can’t find anything there that would clearly detail this point.

  • Do you already have data in the table with null value? Or is this problem only when entering a new data?

  • 1

    It’s just inserting new data. I could simply pass in the controller the default value, but the most ideal would be that even if I did not pass any value either in the request or in the controller the database already set by default as 1 or true.

  • Okay, I put it there. I step into the body of the request basically just the status, so it works, the status is saved in the database. But when I don’t pass it empty it gives this error. Even if I passed empty I wanted the bank to save with the default value.

  • When you pass empty means { "status" : "" } or the body gets {}?

  • the body stays {} i omit the status field in my body

  • Have a related question on Soen, but I don’t know if it’ll help you... Can you confirm in any comic book manager if the status a value default? I use Heidisql to confirm some things

  • 1

    Status Column, the SDBG I’m using is the Dbeaver. Wow, I got it. I took a look at this question you sent. As was said there, I reversed the order and put the defaultValue: 1 on top of the allowNull: false and it worked. I can put the answer or you want to do it?

Show 2 more comments

1 answer

1


Just leave the property defaultValue above the property allowNull in Migration user.

Thank you so much for your time and help @rafael-Tavares

Migration user

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('users', {
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      status: {
        type: Sequelize.BOOLEAN,
+        defaultValue: true,
+        allowNull: false,
      }
    }
  down: (queryInterface) => {
    return queryInterface.dropTable('users');
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.