Let's say I have a simple TypeORM entity like this:
import { Entity, PrimaryGeneratorColumn, Column } from 'typeorm';
@Entity()
export class Employee {
@PrimaryGeneratedColumn()
id: number;
@Column()
Name: string;
}
It all works fine as long as I do not touch database manually. If I enter a record manually, the Entity will not be able to check the last current id from the table and throws error duplicate key value violates unique constraint.
For example:
Employee Entitycreates two records by itself.- I have two records in my database, last record has
id 2 - I create third record in the database manually, it has
id 3 - Afterwards, when
Employee Entityruns, it does not = create another record, since it wants to useid 3, but I added that manually already. - I am getting error
duplicate key value violates unique constraint - When
Employee Entityruns again, it works, because it tries to save another record underid 4already which is available.
So how can I ensure, that Employee Entity checks the last id from the database and then saves the next record with incremental id?
Thank you