Ensure the uniqueness of the incremental value of Sequelize
P粉616111038
P粉616111038 2023-09-06 12:35:12
0
1
288

I currently use Sequelize's increment() function to increment the column of order number values. For example: .increment("order_no", { by: 1 });

Occasionally a request will arrive at my API simultaneously within the same second, resulting in duplicate order number values.

Is there a way to ensure that the incremental value of the order number is unique? Do I need to wrap increment in transaction? After creating an order number, should I randomly and regularly check if there are duplicates? Not sure the correct way to solve this problem.

According to Sequelize’s documentation (https://sequelize.org/docs/v6/core-concepts/model-instances/#incrementing-and-decrementing-integer-values)

In order to avoid concurrency problems, Sequelize provides increment and decrement instance methods to increment/decrement the value of the instance.

However, although it appears that this method works, you will still get duplicate values ​​when used.

P粉616111038
P粉616111038

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!