title: Scoped sequence in postgresql
body: <p>I want to generate unique invoice number per team. 
  I want to have it generated automatically by postgresql database.</p>
  
  <p>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.</p>
  
  <p>Not sure if this is even possible. </p>
  
  <p>Currently, I am creating the next number through code like this
  <code>team.invoices.maximum(:invoice_number).to_i +</code></p>
  
  <p>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.</p>
  
  <p>But, I wanted this to be generated from postgresql itself.</p>