Answer the question
In order to leave comments, you need to log in
How to implement such autoincrement?
Good afternoon!
There is an application on laravel, MySQL database.
It is necessary to implement a table of products, where the id (or some additional id) of the product must be auto-incrementing for each user individually. For example, user 1's product id is 1, 2, 3, user 2's product id is 1, 2, 3, etc.
Autoincrement must be "fair", i.e. by deleting product 3 from user 1, the next product will have id - 4.
How can this be implemented?
The methods I found - a separate table with the counter value for each user, increasing through a trigger (similar question - https://stackoverflow.com/a/18949951 ).
Perhaps there is some more elegant way to solve this issue.
Answer the question
In order to leave comments, you need to log in
Add a simple product counter to each user's profile, and increment it when adding a product for this user.
Auto-increment must be "fair"
You can make a table in which sequences will be stored ("honest" autoincrement :)) and write a function. The user id will be passed to the function input, and the output will be an additional id for this user.
Upd example
. It is not necessary to create a sequence table, as Antonio Solo wrote, you can store this information in the user profile.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question