0

I have an invoice table. It has fields id, oder_id, user_id, invoice_num, I need to increment invoice_num for a specific user each time a user adds information to this table. Those. for a user with id 1 at the first insert it will be equal to 1, at the second insert it will be 2, etc., for a user with id = 2, it will be exactly the same at the first insert 1, at the second 2, etc. I use RLS, if there is any solution using rls it will be generally cool. Please tell me how can I implement this on postgresql.

  • 2
    https://blog.sql-workbench.eu/post/gapless-sequence/ –  Jun 13 '22 at 12:47
  • Does this answer your question? [PostgreSQL sequence based on another column](https://stackoverflow.com/questions/6821871/postgresql-sequence-based-on-another-column) or [How to create sequence based on other column value](https://stackoverflow.com/questions/63357465/how-to-create-sequence-based-on-other-column-value) – Bergi Jun 13 '22 at 13:04
  • See also https://dba.stackexchange.com/questions/135437/create-unique-sequence-per-set-of-rows-sharing-the-same-foreign-key-value and https://stackoverflow.com/questions/24918552/serial-numbers-per-group-of-rows-for-compound-key/24918964#24918964 – Bergi Jun 13 '22 at 13:06

0 Answers0