Search code examples
typeorm

initial AUTO_INCREMENT value for TypeORM PrimaryGeneratedColumn


Is it possible to set the initial AUTO_INCREMENT value for the table with TypeORM PrimaryGeneratedColumn? I tried to read the documentation and code but didn't find anything.


Solution

  • You cannot do it directly from typeorm. But there is nothing stopping you to achieve the same in a migration script. For example, you can tweak the schema of creation migration script like this

    CREATE SEQUENCE public.my_seq
    START WITH 1234
    INCREMENT BY 1
    NO MINVALUE
    MAXVALUE 99999999
    CACHE 1;
    
    CREATE TABLE public."my_schema" (
    number integer DEFAULT nextval('public.my_seq'::regclass) NOT NULL,
    name character varying NOT NULL);
    

    References:

    https://github.com/typeorm/typeorm/issues/2153

    https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver16