I need a string primary column that gets auto-generated when no value is set while still being able to set it myself to whatever value I want. Here's how I handled it on MySQL :
@Entity()
export class Item extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
}
Thing is, I just migrated to PostgreSQL and uuid type prevents me from inserting simple string values. I came up with this workaround but I'm wondering if there is a better way to handle it :
import { v4 as uuid } from 'uuid';
@Entity()
export class Item extends BaseEntity {
@PrimaryColumn()
id: string;
save(options?: SaveOptions): Promise<this> {
if (!this.id) this.id = uuid();
return super.save(options);
}
}
ps it doesn't need to be uuid necessarily. Thanks !
Edit: I need this because I'm getting some items from another db and want to keep their ids as the PK. Hence why I can't use uuid type : the ids might not be uuid at all, just strings. I started using uuid solely because @PrimaryGeneratedColumn('increment')
wouldn't accept string type