'Is there a way to set initial value for auto incremented column in typeorm?

I am using typeorm with postgresql. I want my auto incremented primary key column to start from specific initial value instead of 1. Is there any solution in typeorm for postgresql ?



Solution 1:[1]

TypeORM does not support it, but PostgreSQL does support SEQUENCE (see docs) which can be used to define a custom auto-increment column.

For example, define a SEQUENCE that starts at 50 and increments by 1 each time:

CREATE SEQUENCE
    increment_from_fifty
    start 50
    increment 1;

Then, use it to set the column you want to increment

INSERT INTO sample_table
    (id, title)
VALUES
    (nextval('increment_from_fifty'), 'Title 1');

Anything that can be done with PostgreSQL can be done with TypeORM because you can define custom queries using EntityManager.query() (see docs) or QueryBuilder (see docs).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bernat