I have two tables RevenueGenerator Id(string),name,audience RevenueStream table Id(String),tax,sharepercentage,foreign key to RevenueGenerator (Id)
The entities defined in nestjs are export class RevenueGenerator
@PrimaryColumn()
id: string;
@Column()
name: string;
@Column()
audience: string;
@OneToMany(() => RevenueStream, (revenueStream) => revenueStream.revenueGenerator, { cascade: true })
revenueStreams: RevenueStream[];
export class RevenueStream
@PrimaryColumn()
id: string;
@Column()
tax: string;
@Column()
sharepercentage: string;
@ManyToOne(() => RevenueGenerator, (revenueGenerator) => revenueGenerator.revenueStreams)
revenueGenerator: RevenueGenerator;
This is working fine if i generate the id in nestjs(before insert and populate the id)
But, the id need to be generated autoincrement like RG101,RG102 RS101,RS102 and it should linked the foreign key when i call the create
Can you please guide me how to autoincrement in mysql,with type orm. I tried using trigger but the foreign key is not inserting.
I tried with creating a trigger in mysql with creating a intermediate table with autoincrement id,whenever there is insert it will get the id and prefix. But this is not working when using through nestjs.foreign key is not inserting.
You can use this when you generate string primary key but additionally if you want to add id which is customized that need to be in another column. Which will be generated automatically after each entry of data. Now what you need to do is to make another table to generate a custom id like this.