skip to Main Content

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?

3

Answers


  1. You can use the ‘limit’ option in sequelize:

    findAll({
        limit: 2,
        where: { YOUR QUERY }
    )}
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. If you want to limit records, why don’t do this in the controller / service / business logic part?

    Before creating a record, you can findAll or count the records/rows what the current user have and apply your rule

    Or even, you can add a beforeCreate hook if apply…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search