12

I'm using Sequelize as ORM with a PostgreSQL engine. When using raw queries I can create a table and have columns with 'CHECK' constraints such as

CREATE TABLE products (
    product_no integer,
    name text,
    price numeric CHECK (price > 0)
);

In the docs I cannot find a way to do this in Sequelize when defining models. Is there any way of doing this? I don't want to reinvent the wheel ;)

Thanks!!

3 Answers 3

10
module.exports = {

    up: (queryInterface, Sequelize) => {

        return queryInterface.createTable('Products', {
            product_no: {
                type: Sequelize.INTEGER
            },
            price: {
                type: Sequelize.NUMERIC
            },
            name: {
                type: Sequelize.TEXT
            }
        }).

        then(() => queryInterface.addConstraint('Products', {
            type: 'check',
            fields: ['price'],  // 2024-05-03: This might be needed in the later versions.
            where: {
                price: {
                    [Sequelize.Op.gt]: 0
                }
            }
        }));
    },

    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('Products');
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to declare check constraints in model definition?
0

You can add the db check contraint in the type attribute of the column definition.

sequelize

  sequelize.define(
    'products',
    {
      price: {
        type: 'numeric CHECK (price > 0)',
        ...
      },
    },
  )

sequelize-typescript

  @Column({
    type: 'numeric CHECK (price > 0)', // instead of DataType.NUMBER
    ...
  })
  declare price: number

You can also add them using migrations. See: https://stackoverflow.com/a/51076516/6192751

You can also use js validations to accomplish this on the client side. See: https://github.com/sequelize/sequelize-typescript/issues/576#issuecomment-485383173

Comments

-6

Take a look at the Validations section.

var Product = sequelize.define('product', {
  price: {
    validate: {
      min: 0  // Only allow values >= 0
    }
  }
});

You can also set custom validation rules:

var Product = sequelize.define('product', {
  price: {
    validate: {
      isPositive: function (value) {
        return value > 0;  // Don't allow 0.
      }
    }
  }
});

3 Comments

I believe this only enforces the value at application layer. I don't think this actually puts a constraint on the table. If you're using sequelize migrations, you can run a raw query to insert the constraint yourself.
Agree with the comment above. I was looking into a way to match force-sync and migrations (force-sync is used in circle-ci testing). The solution in the end was to use migrations instead of force-sync in circle-ci, a bit slower but works just as good.
App side validations != Database constraints

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.