Auto incrementing column does not auto-increment
See original GitHub issueIssue 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.
- Configure TypeORM to connect to MariaDB with
mysql
driver. - 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:
- Created 4 years ago
- Reactions:2
- Comments:14 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To follow up on this, it looks like my migration requires the following in the
id
column: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 thisIf you have an idea of making docs better please create PR. We can discuss concrete proposition there.