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?
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
You can use the ‘limit’ option in sequelize:
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.
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
orcount
the records/rows what the current user have and apply your ruleOr even, you can add a
beforeCreate
hook if apply…