I would like to use sequences to create friendly IDs for some objects in my database. My problem is that I don't want the sequence to be global to the table, instead I want to increment the value based on a foreign key.
For example, my table is defined as:
CREATE TABLE foo (id numeric PRIMARY KEY, friendly_id SERIAL, bar_id numeric NOT NULL)
And I would like friendly_id to increment separately for each bar_id such that the following statements:
INSERT INTO foo (123, DEFAULT, 345)
INSERT INTO foo (124, DEFAULT, 345)
INSERT INTO foo (125, DEFAULT, 346)
INSERT INTO foo (126, DEFAULT, 345)
Would result in (desired behavior):
id | friendly_id | bar_id
-----------+------------------+-----------------
123 | 1 | 345
124 | 2 | 345
125 | 1 | 346
126 | 3 | 345
Instead of (current behavior):
id | friendly_id | bar_id
-----------+------------------+-----------------
123 | 1 | 345
124 | 2 | 345
125 | 3 | 346
126 | 4 | 345
Is this possible using sequences or is there a better way to achieve this?
VIEWfor this table that hasrow_number() OVER (PARTITION BY bar_id ORDER BY id) as friendly_id