I want to generate unique invoice number per team. I want to have it generated automatically by postgresql database.
I looked at the documentation for creating sequences in postgresql docs. However, I could not find out how I would specify that, the numbers should be unique on team_id level in the table.
Not sure if this is even possible.
Currently, I am creating the next number through code like this
team.invoices.maximum(:invoice_number).to_i +
To deal with concurrency, I have created unique index on the invoices table (team_id, invoice_number). This will prevent duplicate numbers being created on team level.
But, I wanted this to be generated from postgresql itself.