You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generated type in the mysql database is varchar(255).
As I know this is not efficient way of storing uuid as varchar. It should be possible to store uuid as a binary type. And mysql 8 has buildin functions like: UUID_TO_BIN/BIN_TO_UUID. These function could be used to convert from the human-readable format (char/varchar) to the compact format (binary) and back.
Is it possible to use uuid values for the primary generated column and store it as binary type under the hood?
Reacted by itairaz1, Andrew Willis, Antonio Angelino, Anthony Sendra, Christian Santos Grossi, Darren "Dee" Lee, Álvaro Escarcha, Mark, David Rodrigue, Michael Olšavský and 14 more
We were considering this before, but decided to go simple way because of complexity of implementation and the fact that only mysql 8 support uuid functions.
Reacted by Álvaro Escarcha, Adlan Arif Zakaria, elderapo, Nicolas Fernandes, Grzegorz Tokarz, PokeGuys, Reza Golkhandani, Marcell Alves, Estevão Dias, Simon Knobloch and 33 more
@igat64 you can add a varchar binary key and convert from/to string/Buffer with the event listeners BeforeInsert, BeforeUpdate, AfterUpdate ...
The only problem I have found so far is that this.usersRepository.findOne(id);
does not work with an id of type Buffer, you have to use a workaround e.g.
With @Alia5's workaround I run into this problem caused by the default property not setting id before the Entity Manager tries to reload the entity after a .save().
The Workaround by @Alia5 also doesn't work for me when using relationships. For example when I query a many-to-many relationship, the value transformer is not applied. I created a new issue (10365) for this but did not get any answer. I'd like to fix the problem with a PR but don't know where to start. Help appreciated.
Thanks @Alia5, but we should check in from if bin is actually not null, otherwise the ApplyValueTransformers might fail with this message: TypeError: Cannot read properties of null (reading 'toString').
Activity
pleerock commentedon Jan 8, 2019
We were considering this before, but decided to go simple way because of complexity of implementation and the fact that only mysql 8 support uuid functions.
elovin commentedon Nov 20, 2020
@igat64 you can add a varchar binary key and convert from/to string/Buffer with the event listeners BeforeInsert, BeforeUpdate, AfterUpdate ...
The only problem I have found so far is that
this.usersRepository.findOne(id);does not work with an id of type Buffer, you have to use a workaround e.g.
Alia5 commentedon Nov 13, 2022
To not make this another DenverCoder-like situation
My workaround, short and simple.
Probably also related to #7470
For sake of completion: with relations:
Click me!
maxbethke commentedon Oct 26, 2023
With @Alia5's workaround I run into this problem caused by the
defaultproperty not setting id before the Entity Manager tries to reload the entity after a.save().Any ideas about this?
leonceaklin commentedon Nov 21, 2023
The Workaround by @Alia5 also doesn't work for me when using relationships. For example when I query a many-to-many relationship, the value transformer is not applied. I created a new issue (10365) for this but did not get any answer. I'd like to fix the problem with a PR but don't know where to start. Help appreciated.
hitesh-pathak commentedon Jul 15, 2024
Thanks a lot - this works for me with querybuider and with Many-One and One-Many relations too :D
nalbertcerqueira commentedon Sep 26, 2024
Thanks a lot man ! This saved me :D
mkfyi commentedon Oct 18, 2024
Thanks @Alia5, but we should check in
fromifbinis actually notnull, otherwise theApplyValueTransformersmight fail with this message:TypeError: Cannot read properties of null (reading 'toString').uuid-value.transformer.tsI also wrapped the column definitions into own decorators to reduce the amount of the same column definitions when working with multiple entities.
typeorm.decorators.tsSo your example would look like:
@Entity() export class Room { - @PrimaryColumn({ - type: 'binary', - length: 16, - generated: false, - transformer: uuidTransformer, - default: () => "UNHEX(REPLACE(UUID(), '-', ''))", - }) + @PrimaryGeneratedBinaryUuidColumn() public id!: string; @Column({ length: 100, unique: true }) public name!: string; @OneToMany(() => Member, (member: Member) => member.room) @JoinColumn() public members!: Member[]; } @Entity() export class Member { - @PrimaryColumn({ - type: 'binary', - length: 16, - generated: false, - transformer: uuidTransformer, - default: () => "UNHEX(REPLACE(UUID(), '-', ''))", - }) + @PrimaryGeneratedBinaryUuidColumn() public id!: string; @Column({ length: 100 }) public name!: string; - @Column({ - type: 'binary', - length: 16, - generated: false, - transformer: uuidTransformer, - unique: false, - }) + @BinaryUuidColumn() public roomId!: string; // eslint-disable-next-line @typescript-eslint/no-unsafe-return @ManyToOne(() => Room, (room: Room) => room.members) @JoinColumn() public room!: Room; }festo commentedon Apr 16, 2026
Whoever stumbles upon this, make sure you handle the FindOperators as well.