hreg
hreg

Reputation: 11

How to add an auto increment field to an existing table with TypeORM

This SQL satatement works, and is what I’m trying to generate:

ALTER TABLE `some_entity`
ADD `generatedNumber` INT NOT NULL AUTO_INCREMENT,
ADD UNIQUE (`generatedNumber`);

However I need to do this via TypeORM and this code:

@Entity()
@Unique(['generatedNumber'])
export class SomeEntity extends BaseEntity { //existing entity
  @PrimaryGeneratedColumn('uuid') //existing column
  id: string;

  @Column() //new column trying to create
  @Generated('increment')
  generatedNumber: number;

  @Column({ type: 'text' }) //existing column
  otherColumn: string;

  ...
}

Results in this error:

Error: ER_WRONG_AUTO_KEY: Incorrect table definition; there can be only one auto column and it must be defined as a key

I can see this is because the generated SQL is:

ALTER TABLE some_entity ADD generatedNumber int NOT NULL AUTO_INCREMENT;

It is not adding a unique constraint to the generated sql query.

I’ve Tried:

How can I add this AUTO_INCREMENT with TypeORM?

Upvotes: 1

Views: 56

Answers (0)

Related Questions