question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto incrementing column does not auto-increment

See original GitHub issue

Issue type:

  • question
  • bug report
  • feature request
  • documentation issue

Database system/driver:

  • cordova
  • mongodb
  • mssql
  • mysql / mariadb
  • oracle
  • postgres
  • cockroachdb
  • sqlite
  • sqljs
  • react-native
  • expo

TypeORM version:

  • latest
  • @next
  • 0.x.x (or put your version here)

When using @PrimaryGeneratedColumn, TypeORM isn’t setting the column as AUTO_INCREMENT, meaning that if I try to insert values into the table outside of TypeORM, I get duplicate key query errors.

  1. Configure TypeORM to connect to MariaDB with mysql driver.
  2. Use the following repository/entity

Repository:

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Ingredient {
  @PrimaryGeneratedColumn()
  public id: number;

  @Column('text')
  public name: string;
}

Migration:

import {
  MigrationInterface,
  QueryRunner,
  Table,
} from 'typeorm';

export class createIngredientsTable1555981124297 implements MigrationInterface {
  private tableName: string = 'ingredient';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(new Table({
      name: this.tableName,
      columns: [
        {
          name: 'id',
          type: 'int',
          isPrimary: true,
        },
      ],
    }));
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable(this.tableName);
  }
}

Generated SQL:

CREATE TABLE `ingredient` (
  `id` int NOT NULL,
  PRIMARY KEY (`id`)
)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

62reactions
robertmaincommented, Apr 30, 2019

To follow up on this, it looks like my migration requires the following in the id column:

isGenerated: true,
generationStrategy: 'increment',

However, I’m still leaving this open as I think this may be a documentation issue. My understanding from the documentation was that @PrimaryGeneratedColumn would perform this

4reactions
Kononnablecommented, May 29, 2019

If you have an idea of making docs better please create PR. We can discuss concrete proposition there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Insert auto increment ID where column not ...
I am trying to insert some Data in a Table where the ID is int. The Id is unique and I ...
Read more >
Defining an Auto Increment Primary Key in SQL Server - Chartio
Learn how to define an auto increment primary key in SQL Server. This data tutorial will explain basic table creation and information around...
Read more >
SQL AUTO INCREMENT a Field - W3Schools
AUTO INCREMENT Field. Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.
Read more >
MySQL Tutorial :: 7.9 Using AUTO_INCREMENT
Updating an existing AUTO_INCREMENT column value in an InnoDB table does not reset the AUTO_INCREMENT sequence as it does for MyISAM and NDB...
Read more >
How to Work with Auto-Incrementing IDs in SQL - Retool
The basic syntax for creating this table in MySQL is: CREATE TABLE TableName ( COLUMN1 DataType AUTO_INCREMENT, COLUMN2 DataType, ); In our ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found