Re: Invoice increment

From: Sándor Daku <daku(dot)sandor(at)gmail(dot)com>
To: Søren Frisk <soeren(dot)frisk(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Invoice increment
Date: 2020-02-26 09:22:12
Message-ID: CAKyoTgY2gXYBrd=fiJDTQzwOUsS__Wx_ZJmrV3-KY_m1EBgYjQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 26 Feb 2020 at 09:54, Søren Frisk <soeren(dot)frisk(at)gmail(dot)com> wrote:

> Hi all
>
> I'm trying to find a way to increment an invoice number. And i think it
> would be pretty straight forward just using a SERIAL int. But as we're
> selling across multiple countries, i need the invoice increment to be reset
> by a country_id. any good way to achieve this in a dynamic way?
> Hope this is the right place to ask.
>

Hi,

Instead of a SERIAL field use INTEGER and different sequences for different
countries. You can write a trigger which picks the right sequence based on
the country_id and you can even use a trigger to making the new sequence
for any new country_id.
However it might cause problems if you are not cautious enough. Sequences
and serials not roll back their counter in case of a failing query, so you
could introduce holes into your invoice numbers, which can make supervising
authorities angry. :)

Regards,
Sándor

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vik Fearing 2020-02-26 09:27:21 Re: Invoice increment
Previous Message Paul Förster 2020-02-26 08:40:21 Re: Highly academic: local etcd & Patroni Cluster for testing on a single host