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
{{ message }}
Repository navigation
@Column({ type: "uuid" }) should be varchar(36) on MySQL instead of varchar(255) #7228
When I create a uuid column with @Column({ type: "uuid" }) it's using varchar(255)
The Solution
I've dealt with this for now by using @Column({ type: "uuid", length: 36 }) but it seems like a simple change to make 36 the default length for uuid columns. If I threw up a PR for that, would anybody object?
Relevant Database Driver(s)
aurora-data-api
aurora-data-api-pg
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, and I know how to start.
Yes, I have the time, but I don't know how to start. I would need guidance.
No, I don't have the time, although I believe I could do it if I had the time...
No, I don't have the time and I wouldn't even know how to start.
This is a serious breaking change and is creating serious issues with migrations. Now that the default for UUID is char(32), previously existing varchar(32) columns are being dropped and replaced in migration generation scripts, with all the corresponding effects (really bad).
At the very least, detection of an existing varchar field of the same length should be detected and left alone. Alternatively and probably better would be the ability to add an option to choose between char / varchar rather than it being forced. Migrating all primary keys in tables between types is less than ideal and a show stopper for many reasons.
I can create a new ticket, but it seems this one should be opened back up and fixed?
Migrating all primary keys in tables between types
This doesn't affect primary keys. Just UUIDs.
My suggestion would be to write a migration that you would prefer instead of using the automatically generated one.
The automatically generated ones err on the side of EXACTNESS which may or may not be correct.
Please feel free to open another issue if you believe something needs to be done differently.
It does if your primary key uses the primary column uuid generation strategy. Per the docs:
@PrimaryGeneratedColumn("uuid")
id: string;
- @PrimaryGeneratedColumn("uuid") creates a primary column which value will be automatically generated with uuid. Uuid is a unique string id.
You don't have to manually assign its value before save - value will be automatically generated.
https://github.com/typeorm/typeorm/blob/master/docs/entities.md
Certainly you want to hand check everything, but migration script generation serves a very important purpose in some situations and create a nice baseline for hand written scripts...when they are mostly accurate.
My previous recommendation to allow the underlying db type to remain unchanged (through configuration) seems appropriate, as a varchar(36) is completely compatible with UUID fields and had been used up until this change. The change to move to a char(36) is an incremental good that brought a breaking change for some folks, but it is easily rectified.
Thanks for your consideration.
On Fri, Jul 30, 2021, 11:56 PM James Ward ***@***.***> wrote:
Migrating all primary keys in tables between types
This doesn't affect primary keys. Just UUIDs.
My suggestion would be to write a migration that you would prefer instead
of using the automatically generated one.
The automatically generated ones err on the side of EXACTNESS which may or
may not be correct.
Please feel free to open another issue if you believe something needs to
be done differently.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#7228 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABXYKSLOOHYR5LNUSS2QALT2OGBNANCNFSM4VNYXECA>
.
Got it - it switches it from a 36 character varchar to a 36 character char.
You're right that it's a baseline for hand written changes. However, not all changes are easy to write generations for.
In particular, what you're describing sounds less like a problem with this change exactly and more with a problem in how we do changes to columns. In this case an ALTER COLUMN to set the type would be more appropriate than dropping & creating a new column.
Yeah, an alter column would have been a preferable and less destructive change.
Still, it's also a lot of noise during each migration generation, particularly if one were to want to leave the varchar(32) and not change the column type. Seems like both changes would be good to have. If you are amenable to a flag to allow varchar (old behavior), I can help with a PR. The advantage here is that even if folks are only using schema generation for something like testing, they can get still things to match up without having to migrate to the new column types. This is nice for folks that are trying to make minimal changes in production setups.
@imnotjames we should try to avoid these kind of changes, because they are considered as breaking. If user don't have a data backup and accidentally run it on production - it's a big problem. I would recommend to publish a new version with revert of this fix.
Before 0.2.0 we had ALTER COLUMN approach instead of drop/create for the columns with "changed type", but we had so many issues and complexity because of them, that we decided with @AlexMesser to keep it simple - if column type has completely changed - drop and re-create the column. I don't think we should go back to ALTER COLUMN, at least for now.
We can figure out a new mechanism (for example, cli command "typeorm upgrade") - that can solve these kind of issues.
this is serious breaking change, it directly affects our production database after syncing. Previously default for uuid is varchar(36) now you changed to char(36) without any backward compatibility, we synced, and all the columns with uuid are cleared
I mean we sync on our sandbox database and it dropped a bunch of primary keys and then blew up errors. Thankfully we were able to restore data from backups, but this a terrible side effect of what should really be called out and labeled as a major breaking change
A little heads up for people in here: VARCHAR() stores the length as 1 byte, so there's no storage or performance difference between VARCHAR(36) and VARCHAR(255).
Also, VARCHAR(255) may actually save space because CHAR(36) will always allocate a length of 36, because no length is stored.. mysql will always allocate 36. (Real life example: an unused foreign key will have 36 allocated anyway).
A little heads up for people in here: VARCHAR() stores the length as 1 byte, so there's no storage or performance difference between VARCHAR(36) and VARCHAR(255).
Also, VARCHAR(255) may actually save space because CHAR(36) will always allocate a length of 36, because no length is stored.. mysql will always allocate 36. (Real life example: an unused foreign key will have 36 allocated anyway).
Any plans on fixing this? Is there an upcoming major release where the CHAR(36) type for UUIDs would make sense? Maybe this could be configurable to avoid breaking people's migration generation.
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.
Activity
yf-hk commentedon Jan 14, 2021
What about
char(36)?varcharmeans variable lenth character.dickfickling commentedon Jan 14, 2021
good point, char(36) makes more sense
arimus commentedon Jul 31, 2021
This is a serious breaking change and is creating serious issues with migrations. Now that the default for UUID is char(32), previously existing varchar(32) columns are being dropped and replaced in migration generation scripts, with all the corresponding effects (really bad).
At the very least, detection of an existing varchar field of the same length should be detected and left alone. Alternatively and probably better would be the ability to add an option to choose between char / varchar rather than it being forced. Migrating all primary keys in tables between types is less than ideal and a show stopper for many reasons.
I can create a new ticket, but it seems this one should be opened back up and fixed?
imnotjames commentedon Jul 31, 2021
This doesn't affect primary keys. Just UUIDs.
My suggestion would be to write a migration that you would prefer instead of using the automatically generated one.
The automatically generated ones err on the side of EXACTNESS which may or may not be correct.
Please feel free to open another issue if you believe something needs to be done differently.
arimus commentedon Jul 31, 2021
imnotjames commentedon Jul 31, 2021
Got it - it switches it from a 36 character
varcharto a 36 characterchar.You're right that it's a baseline for hand written changes. However, not all changes are easy to write generations for.
In particular, what you're describing sounds less like a problem with this change exactly and more with a problem in how we do changes to columns. In this case an
ALTER COLUMNto set the type would be more appropriate than dropping & creating a new column.arimus commentedon Aug 1, 2021
Yeah, an alter column would have been a preferable and less destructive change.
Still, it's also a lot of noise during each migration generation, particularly if one were to want to leave the varchar(32) and not change the column type. Seems like both changes would be good to have. If you are amenable to a flag to allow varchar (old behavior), I can help with a PR. The advantage here is that even if folks are only using schema generation for something like testing, they can get still things to match up without having to migrate to the new column types. This is nice for folks that are trying to make minimal changes in production setups.
pleerock commentedon Aug 2, 2021
@imnotjames we should try to avoid these kind of changes, because they are considered as breaking. If user don't have a data backup and accidentally run it on production - it's a big problem. I would recommend to publish a new version with revert of this fix.
Before 0.2.0 we had
ALTER COLUMNapproach instead of drop/create for the columns with "changed type", but we had so many issues and complexity because of them, that we decided with @AlexMesser to keep it simple - if column type has completely changed - drop and re-create the column. I don't think we should go back toALTER COLUMN, at least for now.We can figure out a new mechanism (for example, cli command "typeorm upgrade") - that can solve these kind of issues.
maitrungduc1410 commentedon Sep 24, 2021
this is serious breaking change, it directly affects our production database after syncing. Previously default for uuid is
varchar(36)now you changed tochar(36)without any backward compatibility, we synced, and all the columns with uuid are clearedyf-hk commentedon Sep 24, 2021
Well, it's a bad practice to sync on a production database... But it's surely an issue. Maybe we need to tag it to a different major version
devshorts commentedon Sep 28, 2021
I mean we sync on our sandbox database and it dropped a bunch of primary keys and then blew up errors. Thankfully we were able to restore data from backups, but this a terrible side effect of what should really be called out and labeled as a major breaking change
4 remaining items
ArielPrevu3D commentedon Jan 17, 2023
Here's a workaround to bring back
char, for the brave among us. Don't use this outside of migration code.gnat commentedon May 8, 2023
A little heads up for people in here:
VARCHAR()stores the length as 1 byte, so there's no storage or performance difference betweenVARCHAR(36)andVARCHAR(255).Also,
VARCHAR(255)may actually save space becauseCHAR(36)will always allocate a length of 36, because no length is stored.. mysql will always allocate 36. (Real life example: an unused foreign key will have 36 allocated anyway).Also, lol @ everyone getting primary key migrations. Yeah.. that's bad.
ArielPrevu3D commentedon May 8, 2023
VARCHAR(255)will never save space compared toCHAR(36)for this usecase.gnat commentedon May 8, 2023
The devil is in the details. @ArielPrevu3D See: https://dev.mysql.com/doc/refman/8.0/en/char.html
Your unused reference / relational fields are going to take 36 bytes if
CHAR(36). Only 1 byte ifVARCHAR(255). 👀ArielPrevu3D commentedon May 18, 2023
The unused relation fields are going to be
NULL.ArielPrevu3D commentedon Oct 6, 2023
Any plans on fixing this? Is there an upcoming major release where the
CHAR(36)type for UUIDs would make sense? Maybe this could be configurable to avoid breaking people's migration generation.Rayyan98 commentedon Nov 29, 2023
Shouldn't we just go to binary 16 now, in a configurable manner of course in the decorator
github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.
Rayyan98 commentedon Sep 19, 2025
Would love to have a binary 16 column option behind this so i can off load some logic from my application to it