Home > OS >  How to apply limit on sequlize table rows number?
How to apply limit on sequlize table rows number?

Time:10-27

I want to apply limit on Sequelize table declaration. This would actually limit the rows total of the table to 1 by forbidding to add more rows into it. How can I achieve this in the table model definition?

CodePudding user response:

I don't think that it is a good idea to use database table with only one row possible.

But a technical solution could be to use a default primary key (e.g. id: 1). This primary key won't be auto increment or anything but a static default value like integer 1. So that you could not add any other rows than the very first one, as a primary key is unique and it's value is always equals to 1. So you would only be able to update or read its value, if that's why you're trying to achieve.

But again this sounds like an anti-SQL thing to do.

CodePudding user response:

You can use the 'limit' option in sequelize:

findAll({
    limit: 2,
    where: { YOUR QUERY }
)}
  • Related