Search code examples
mysqldatabasetypeormnode.js-typeorm

How to create autoincrement integer field in TypeORM migration?


I design a table and want an integer autoincrement column for an ID. For sure, I read the docs about isGenerated param for migrations.

Now, my code looks like this:

public async up(queryRunner: QueryRunner): Promise<void> {

        await queryRunner.createTable(
            new Table({
                name: tableName,

                columns: [
                    {
                        name: "id",
                        type: "int",
                        isGenerated: true,
                        isPrimary: true,
                    },
                    {
                        name: "seller_id",
                        type: "int"
                    }
                ]
            })
      )
}

But as a result, I got: WorkBench

As you can see, id is not flagged as AI. What I'm doing wrong?


Solution

  • {
        name: "id",
        type: "int",
        isPrimary: true,
        isGenerated: true,
        generationStrategy: "increment"
     },
    

    available options:

    generationStrategy?: "uuid" | "increment" | "rowid" | "identity";