Muhammad Hammad Ahmad
Muhammad Hammad Ahmad

Reputation: 89

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 ?

Upvotes: 1

Views: 1635

Answers (1)

Bernat
Bernat

Reputation: 554

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).

Upvotes: 1

Related Questions